Array initialisation (zeroing, loading defaults, filling patterns) is a standard program duty: once at power-up, once at reset. A FOR loop is the general method; block instructions (FILL/MEMSet) are the efficient one — know both and choose by situation. The sections below cover the three patterns, the execution timing, and efficiency and taboos.
| Pattern | Coding (ST) | Use |
|---|---|---|
| Zero everything | FOR i:=1 TO 100 DO Data[i]:=0; END_FOR; | Power-up/reset buffer clear |
| Load defaults | FOR i:=1 TO 20 DO Recipes[i].Temp:=Default[i]; END_FOR; | Default recipe table |
| Fill a pattern | FOR i:=1 TO 50 DO Data[i]:=i; END_FOR; | Index tables, test sequences |
Block equivalents: pure zeroing is one FILL/FMOV/MEMSet (far faster than a loop); "load defaults" becomes a block copy when the defaults themselves are tabular (BMOV default table → array); pattern fills have no block substitute — FOR is the answer.
Initialisation belongs on the "first scan after power-up" (first-scan flag / first-scan bit) or inside the reset flow — running it every scan is a major error: the loop keeps刷ing user-edited data back to defaults, and the field symptom is "parameters bounce back after I change them". Checklist: the trigger for initialisation must be a one-shot event (power-up edge, reset-button edge), never a always-true condition.
| Timing | Trigger | Typical content |
|---|---|---|
| Once at power-up | First-scan flag | Array clear, checksum check, default recipes |
| Once at reset | Reset button edge | Clear runtime data, reload defaults |
| Once at mode switch | Switch edge | Clear mode-related arrays (manual trace buffer) |
Loop cost ≈ iterations × instructions per iteration. A 1000-iteration clear in the main program may cost tens of microseconds to milliseconds — as a constant scan cost it matters; as a rare power-up event it does not. The principle: initialisation inside rare events (power-up/reset) may loop freely; anything on the constant scan path uses block instructions. Even block instructions on the constant path deserve scrutiny — re-flushing a large array every scan is a cost too.
One efficiency trick: for arrays needing repeated initialisation, switch to "incremental marking" — initialise only what was used (track a last_used index) instead of flushing everything.
Two-dimensional arrays (10 recipes × 20 parameters) take a double FOR: recipes outside, parameters inside, with distinct loop variables (i, j) — never reused. Three boundary rules: ① loop bounds match the array declaration exactly (ARRAY[1..20] means TO 20 — overshoot either errors out or silently corrupts a neighbour, platform-dependent); ② whether indexes start at 0 or 1 differs by platform — declaration and access must agree; ③ loop variables are integers, never floating point (precision plus efficiency, both lost).
| Symptom | Cause | Fix |
|---|---|---|
| Parameters bounce back after editing | Initialisation runs every scan | Trigger on a one-shot event |
| Occasional scan-time spikes | Large-array loop on the constant path | Move to rare events or use block instructions |
| Tail of the array corrupted | Loop upper bound overruns into a neighbour | Match bounds to the declaration |
| Initialisation seems not to run | Trigger condition never true | Monitor the trigger flag and loop variables |
| Double loop scrambles results | Inner and outer share one variable | Separate i and j |
The craft in array initialisation is not FOR syntax but "timing" and "boundaries": initialisation only on one-shot events (or user data gets flushed), bounds strictly matching the declaration (or neighbours get corrupted). Hold both and the FOR initialisation is a clean tool; lose either and it manufactures "mysteriously lost data" and "scan spikes".
FOR loops and arrays follow IEC 61131-3; block-fill instructions (FILL/FMOV/MEMSet) follow the platform's applied-instruction manual; first-scan flag behaviour follows each platform's system-device documentation; out-of-bounds behaviour follows the platform runtime (checked platforms error; unchecked ones corrupt neighbours at your own risk).
After writing initialisation, run two tests: edit a parameter, power-cycle immediately (retained or default per design), and monitor the loop variable's first and last values (they must equal the declared bounds).