# Standard and utility function blocks

> Inputs, outputs and behaviour of the IEC 61131-3 standard and utility function blocks of eaxPLC (timers, counters, edge detectors, PID, generators, scaling).

Product: eaxPLC · Type: Reference · Editions: Free, Pro
Source: https://eaxware.com/docs/eaxplc/reference/function-blocks/
Status: migrated page, not yet verified against the current code.

Pinout, timing and formulas of the IEC 61131-3 function blocks implemented in `pkg/plc/fb/`.

This hand-written page covers 18 of the 29 native blocks and none of the Structured Text library blocks. The generated function block reference, which covers every block, will replace it.

---

## 1. Standard Library (`Standard.lib`)

**Diagram: TON, TOF and TP over the same input** (timeline)

The same input IN drives the three timers, with a preset time PT of 20 units on this axis. IN is TRUE for 30 units, then for 8 units. TON turns Q on PT after IN rises and off when IN falls, so the short pulse gives no output. TOF keeps Q on until PT after IN falls. TP gives a pulse of exactly PT at each rising edge, even when IN is shorter.

**Input**

- **IN**: the same for all three

**TON, on-delay**

- **Q**. TRUE from PT after IN rose until IN falls. The second IN pulse, shorter than PT, gives no output.
- **ET**. ET is 0 as soon as IN is FALSE.

**TOF, off-delay**

- **Q**. TRUE while IN is TRUE and until PT after IN falls.
- **ET**. ET counts from the falling edge of IN and reaches PT when Q turns off. It holds PT until IN rises again, which sets it to 0.

**TP, pulse**

- **Q**. A pulse of exactly PT from each rising edge of IN, however long IN stays TRUE.
- **ET**. At the end of the pulse ET stays at PT while IN is TRUE, and is 0 once IN is FALSE.

ET counts while the time runs. After its delay, TOF's ET holds PT until IN rises again.

### 1.1 `TON` (On-Delay Timer)
Delays turning on an output signal for a preset time interval `PT`.
- **Inputs**:
  - `IN: BOOL`: Trigger input
  - `PT: TIME`: Preset delay duration
- **Outputs**:
  - `Q: BOOL`: Output turned ON when elapsed time `ET >= PT` while `IN = TRUE`
  - `ET: TIME`: Elapsed time counter

### 1.2 `TOF` (Off-Delay Timer)
Keeps output `Q` energized for a preset time `PT` after input `IN` drops to `FALSE`.
- **Inputs**: `IN: BOOL`, `PT: TIME`
- **Outputs**:
  - `Q: BOOL`: `TRUE` while `IN = TRUE`, and for `PT` after `IN` falls
  - `ET: TIME`: `0` while `IN = TRUE`; counts from the falling edge of `IN`; after the delay it holds the value it reached (the `PT` in force then) until `IN` rises again, as IEC 61131-3 specifies, even if `PT` changes meanwhile. It is `0` before the first delay and after a reset.

Earlier eaxPLC versions set `ET` back to `0` in the cycle after `Q` turned off. A program that tests `TOF.ET = 0` to detect the end of the delay now sees `ET = PT` instead: test `NOT Q` for the end of the delay.

### 1.3 `TP` (Pulse Timer)
Generates a pulse of fixed duration `PT` regardless of how long `IN` remains `TRUE`.
- **Inputs**: `IN: BOOL`, `PT: TIME`
- **Outputs**: `Q: BOOL`, `ET: TIME`

### 1.4 `R_TRIG` & `F_TRIG` (Edge Detectors)
- `R_TRIG`: Output `Q` produces a single-cycle pulse on the rising edge of `CLK` (`FALSE -> TRUE`).
- `F_TRIG`: Output `Q` produces a single-cycle pulse on the falling edge of `CLK` (`TRUE -> FALSE`).

### 1.5 `SR` & `RS` (Bistable Flip-Flops)
- `SR` (Set-dominant): `Q1 := (NOT RESET AND Q1) OR SET1`
- `RS` (Reset-dominant): `Q1 := NOT RESET1 AND (Q1 OR SET)`

### 1.6 `CTU`, `CTD`, `CTUD` (Counters)
- `CTU`: Increments `CV` on rising edge of `CU`. `Q := CV >= PV`.
- `CTD`: Decrements `CV` on rising edge of `CD`. `Q := CV <= 0`.
- `CTUD`: Bidirectional counter with separate up/down triggers, `LOAD`, and `RESET`.

---

## 2. Utility Library (`Util.lib`)

### 2.1 `BLINK` (Asymmetric Pulse Generator)
Generates continuous square-wave pulses with distinct high and low intervals.
- **Inputs**: `ENABLE: BOOL`, `TIMELOW: TIME`, `TIMEHIGH: TIME`
- **Outputs**: `OUT: BOOL`

### 2.2 `GEN` (Waveform Generator)
Generates analog waveforms with selectable mode:
- `MODE 1`: Triangle wave
- `MODE 2`: Positive triangle wave
- `MODE 3`: Sawtooth rising
- `MODE 4`: Sawtooth falling
- `MODE 5`: Sine wave (`sin(2π·t/T) × AMPLITUDE`)
- `MODE 6`: Cosine wave (`cos(2π·t/T) × AMPLITUDE`)
- `MODE 7`: Rectangle / square wave

### 2.3 `PID` & `PID_FIXCYCLE` (Closed-Loop Controllers)
Implements standard parallel PID control law:
```
Y = KP · ( e(t) + (1/TN) · ∫ e(t) dt + TV · de(t)/dt ) + Y_OFFSET
```
- Anti-windup clamping to `[Y_MIN, Y_MAX]`.
- Manual override mode with `Y_MANUAL`.
- `LIMITS_ACTIVE` and `OVERFLOW` diagnostic flags.

### 2.4 `LIN_TRAFO` (Linear Scaling)
Transforms input value from `[IN_MIN, IN_MAX]` into `[OUT_MIN, OUT_MAX]`:
```
OUT = OUT_MIN + (IN − IN_MIN) / (IN_MAX − IN_MIN) × (OUT_MAX − OUT_MIN)
```

### 2.5 `RAMP_REAL` & `RAMP_INT` (Rate-of-Change Limiters)
Limits the maximum upward (`ASCEND`) and downward (`DESCEND`) rate of change per unit time `TIMEBASE`.

### 2.6 `HYSTERESIS` (Schmitt Trigger)
Prevents signal chatter around a threshold:
- `OUT := TRUE` when `IN >= HIGH`
- `OUT := FALSE` when `IN <= LOW`
- Retains previous state when `LOW < IN < HIGH`

### 2.7 `LIMITALARM` (Deadband Alarm)
- `O: BOOL`: High alarm (`IN > HIGH`)
- `U: BOOL`: Low alarm (`IN < LOW`)
- `IL: BOOL`: In-limit indicator (`LOW <= IN <= HIGH`)

---

## 3. Arrays of function block instances

Every block on this page, and every user `FUNCTION_BLOCK`, can be declared as an array. Each element is a full instance with its own state, called and read like a named instance. A 2-D array is an array of arrays: `g[i, j]` and `g[i][j]` are the same element. An index outside the bounds stops the scan with "array index out of range".

```text
VAR
    t    : ARRAY[0..7] OF TON;
    g    : ARRAY[1..2, 1..3] OF FB_Valve;
    i, j : INT;
END_VAR

FOR i := 0 TO 7 DO
    t[i](IN := Run[i], PT := T#2s);
    Done[i] := t[i].Q;
END_FOR;
FOR i := 1 TO 2 DO FOR j := 1 TO 3 DO g[i, j](); END_FOR; END_FOR;
```

A safety target can name an element's member (`t[3].Q`, `g[1, 2].Cmd`). The code generation, the cost per scan and the safety-target rules are in [Compiler and bytecode VM](/docs/eaxplc/explanation/compiler-and-vm/#4-arrays-of-function-block-instances).
