0

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.

Picky33
  • 11
  • 2
  • Your question is unclear. Are you talking about leading edge detection ? If yes, then you need to track the state. Usually this is done with a shift register. Bump value A to reg 2 and take new value B into reg 1 then compare. – wbg Mar 18 '21 at 22:17

2 Answers2

1

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
Jakob
  • 1,288
  • 7
  • 13
0

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);
}
Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38