Search code examples
triggerspersistentplcretainst

ST PLC programming: Detecting a rising edge and remembering its value


I’ve started working in PLC structured text programming recently. Excuse me for the noobie question but how can I retain a rising edge detectors value?

I am thinking of an RS flip flop of some kind.

I am using codesys v2.3 and there is a function block (R_TRIG) for detecting a signal change from false to true. However the output signal from the function block lasts for about 1 second (no matter the cycle times -> I have tried it with really high values).

I have tried to retain the value with a simple if:

IF Rtrigger.Q = true THEN Triggermemory := true;

However the new variable (Triggermemory) follows the edge trigger output value (of course).

Is there any way to retain the once true signal from an edge detector??

I have tried using retain and persistent global variables (no success), but I think the real problem is that I somehow need to store this true value in somewhere.


Solution

  • According to your code "Triggermemory" should not follow the edge trigger output variable.

    It should only be set when there is a raise condition as you put here. IF Rtrigger.Q = true THEN Triggermemory := true; END_IF

    Is this code inside a function, function block or program? If you have declare your RTrigger inside a function, it will be "created" every time you enter the function and "destroyed" every time you exit the function. So the RTrigger wouldn't work as you expect.

    If this code is in a function, you might want to consider creating it as a global variable.

    However If all you want to do is to check if a variable ever became TRUE, did you consider using a bistable SR? This will become TRUE if at any point the variable becomes TRUE. Even if the variable goes back to be false it will stay TRUE.

    Then you can use the "Reset" input to force it to be back to a reset position (FALSE).

    Regarding "persistent global variables", these variables are useful when you want your variables to survive a reset.