OSRI / OSFI — One Shot Rising and Falling with Input
One Shot Rising with Input (OSRI). Structured Text one-shot function block that sets OutputBit for exactly one scan when InputBit goes from 0 to 1. Uses FBD_ONESHOT; the ST equivalent of ladder OSR.
One Shot Falling with Input (OSFI). Structured Text one-shot function block that sets OutputBit for exactly one scan when InputBit goes from 1 to 0. Uses FBD_ONESHOT; the ST equivalent of ladder OSF.
An edge is the single scan a signal changes — not the state itself. A one-shot turns "the button is held down" into "the button was pressed just now," so counters count once per press instead of once per scan.
Operands
| Name | Type |
|---|---|
oneShot | FBD_ONESHOT |
Each edge you watch needs its own FBD_ONESHOT tag — the structure remembers the previous scan's InputBit internally.
FBD_ONESHOT Tag Members
| Member | Type | Meaning |
|---|---|---|
.EnableIn | BOOL | Forced to 1 each scan by the instruction call — setting it 0 has no effect |
.InputBit | BOOL | Input the one-shot watches — assign it before calling the instruction each scan |
.EnableOut | BOOL | 1 while the instruction is executing normally |
.OutputBit | BOOL | 1 for exactly one scan after the watched InputBit edge — rising for OSRI, falling for OSFI |
How They Work
Each scan, the instruction compares InputBit now against what it was on the previous scan:
OSRI—InputBitgoes 0 → 1:OutputBitis1for that one scan. WhileInputBitstays 1 (or stays 0),OutputBitis0.OSFI—InputBitgoes 1 → 0:OutputBitis1for that one scan. A falling edge is not the same as "the input is off" —OSFIpulses only on the scan the input turns off.
Call the instruction every scan and assign .InputBit before the call — the edge is detected at the moment of the call:
EdgeTag.InputBit := In_Signal;
OSRI(EdgeTag);
Out_Pulse := EdgeTag.OutputBit;
Neither instruction fires on the first scan for a signal that was already settled at startup. Prescan arms the internal edge memory — OSRI treats an input that is already 1 as old news, and OSFI has no stored "1" for a falling edge to measure against.
InputBit _____|‾‾‾‾‾‾‾‾|________|‾‾‾‾‾‾|___
OSRI _____|‾|_______________|‾|________
OSFI _____________|‾|______________|‾|_
Example — Count Button Presses
OSRI fires once per press no matter how long the button is held:
One_Press.InputBit := In_Button;
OSRI(One_Press);
IF One_Press.OutputBit THEN
Val_Count := Val_Count + 1;
END_IF;
Example — Act When a Machine Stops
OSFI catches the moment In_Running drops, latching a done flag exactly once per cycle:
One_Stopped.InputBit := In_Running;
OSFI(One_Stopped);
IF One_Stopped.OutputBit THEN
Out_CycleDone := 1;
END_IF;
Common Mistakes
- Reading
.OutputBiton a line above the instruction call — you get the previous scan's pulse, one scan late. Assign.InputBit, call the instruction, then read.OutputBit, in that order. - Reusing one
FBD_ONESHOTtag for two different signals (or calling the instruction twice on the same tag in one scan) — the second use consumes the edge memory. One tag per watched edge. - Confusing a falling edge with an inverted input —
NOT In_Signalis1the whole time the signal is off;OSFIpulses for a single scan when it turns off. - Expecting a pulse on the first scan because the input starts at 1 — prescan arms the edge memory, so a settled input is not an edge.
OSFIneedsInputBitobserved at 1 during a scan before the drop to 0 counts.