I’m trying to think a generalized way to program something that is True( does some logic) only when a bit goes from high to low.
The programming language doesn’t matter but I prefer JAVA or Structured Text.
Any help would be appreciated.
I’m trying to think a generalized way to program something that is True( does some logic) only when a bit goes from high to low.
The programming language doesn’t matter but I prefer JAVA or Structured Text.
Any help would be appreciated.
Within the IEC61131-3 world using ST this can be achieved using F_TRIG (Falling trigger).
Simply declare an F_TRIG and call it with a boolean and check if F_TRIG is enabled (Q):
VAR
yourFTrig : F_TRIG;
END_VAR
------------------------
yourFTrig(CLK := yourBoolean);
IF yourFTrig.Q THEN
// Your code that shall be executed only when we have a falling trigger
END_IF
In order to detect rising or falling edge, program have to work in cycles. In PLC IEC 61131-3, it already works in cycles. There are 2 ways to detect edge. One is already described by @Jacob. It is to use F_TRIG
and R_TRIG
function blocks.
Let me demonstrate underlying principle of this logic.
PROGRAM demo
VAR
xStart : BOOL; (* Bit that we detect edge on *)
xStartM: BOOL; (* Bit to store value from last cycle *)
END_VAR
IF xStart AND NOT xStartM THEN
(* Rising edge detected *)
END_IF
IF NOT xStart AND xStartM THEN
(* Falling edge detected *)
END_IF
xStart := xStartM;
END_PROGRAM;
Pay attention to xStart := xStartM;
. Everything that is above this line will have xStartM
equal to value of xStart
from the last cycle. It means that if we use ёШАё before that line we may find that xStart
is TRUE
but xStartM
still not, thus it is the moment we switch.
In JAVA in order to that you use the same technic, but you have to do it all in a while. I do not know JAVA, but here is how it could look in PHP.
<?php
$am = false;
while(true) {
$a = getA();
if ($a && !$am) {
// rise edge
}
if (!$a && $am) {
// fall edge
}
$am = $a;
sleep(1);
}