Interrupts are the "don't wait for the scan, handle it now" channel: timed interrupts insert on a fixed period, external interrupts trigger on an input's edge. They solve different problems — timed interrupts handle "it is time, must do", external interrupts handle "the signal arrived, do it now". Used right they achieve what the scan cannot; used wrong they become a priority disaster. The sections below compare mechanisms, uses, discipline and taboos.
| Aspect | Timed interrupt | External interrupt |
|---|---|---|
| Trigger source | Timer expiry (e.g. every 10/20 ms) | Edge on a designated input X |
| Timing | Fixed period, asynchronous to the scan | Inserts as the signal edge arrives |
| Typical carrier | FX I6□□ timer interrupts; IEC platform tasks | FX I0□0~I5□0 input interrupts (X0~X5) |
| Response latency | ≤ 1 interrupt period | Sub-millisecond (versus scan times of several to tens of ms) |
| Right for | Periodic sampling, comms polling, pulse-width control | Encoder Z phase, home signal, capture-type instant events |
The rule of thumb: events arriving on a time schedule → timed interrupt; events decided by an external instantaneous signal → external interrupt; neither is urgent → ordinary scan code, and leave interrupts alone.
① Fixed-period sampling: temperature and pressure every 10 ms for filtering — scan jitter does not bend the sample rhythm, so filter windows stay true; ② pulse-width / proportional control: duty computed on a 10 ms cycle, steadier than scan code; ③ communication polling: high-priority slave exchanges on schedule; ④ time accumulation decoupled from the scan: run-seconds counted on true time (scan-based timers drift when long tasks stretch the cycle).
Setting rule: the interrupt period must clearly exceed the interrupt program's execution time (generally ≤ 30% of the period), or the interrupt re-triggers continuously, starves the main program, and the scan time balloons into a watchdog trip.
① Home-signal capture: on servo/stepper homing, latch the current position the instant the home switch edges — far more accurate than scan polling; ② encoder Z-phase counting: revolutions accumulate on the Z edge without losing counts; ③ instant event logging: time-stamping microsecond-to-millisecond events like probe trips or camera exposure-done. The external interrupt's creed is "in fast, out fast": the interrupt only latches (move the needed data, set a flag) and all processing waits for the main scan.
| Rule | Reason |
|---|---|
| Short: a few to a few dozen instructions | Interrupt time is deducted directly from the main scan |
| Latch only, process never | Processing belongs to the main program; the interrupt moves data and sets flags |
| Dedicated data area | Interrupt and main both writing one device = a race; values may tear |
| No long instructions | Comms instructions or bulk moves in an interrupt freeze the main program |
| Flag handshake | Interrupt sets "new data"; main processes and clears the flag |
On "dedicated data area": the interrupt writes D100 (position latch), the main reads it and never writes; the main writes its consumed result to D110 — writers separated, readers free. Sharing one D for both read and write occasionally delivers a half-written value, a race bug that is rare and nearly impossible to reproduce.
① Ordinary buttons and in-position signals: a scan cycle of a few milliseconds is imperceptible — interrupts add complexity for nothing; ② control logic sharing lots of state across places: interrupt isolation makes synchronisation harder; ③ cases that need "certainty" more than "speed": timed sampling with flag handshakes beats interrupts for controllability. The principle: confirm the scan genuinely cannot do it before reaching for interrupts — they are special equipment, not standard kit.
| Symptom | Cause | Fix |
|---|---|---|
| Main scan time balloons | Interrupt program too long / period too short | Slim the interrupt, widen the period |
| Watchdog trips | Interrupt re-triggers continuously, saturating the CPU | Period ≥ 3× execution time |
| Latched value occasionally wrong | Interrupt and main writing the same device | Dedicated areas + flag handshake |
| Interrupt never fires | Interrupt not enabled / input filter too coarse | Enable per manual, reduce the input filter |
| Glitches trigger false interrupts | Fast signal line picking up noise | Shield earthing + hardware filter setting |
Both interrupt types share one philosophy: "in fast, out fast". The interrupt only seizes the instantaneous fact (time's up / signal came) and its data; judgement and action all stay in the main program. Hold that boundary and interrupts are a speed tool; cross it and they become a random-fault generator.
Interrupt instructions, pointers and settings follow the PLC's programming manual (e.g. Mitsubishi FX interrupt functions, Q-series I/O and timer interrupts); IEC platforms correspond to tasks and event mechanisms in the CODESYS/TwinCAT task documentation; the relation of interrupts to safety follows GB 5226.1 / IEC 60204-1 — safety response never depends on PLC interrupts; the safety circuit stays hard-wired.
Before adding any interrupt, do two sums: the interrupt's execution time and the main scan's headroom — three times margin each, and the interrupt stays obedient.