Edge Detection Function Blocks

Help Contents

Introduction

Two standard function edge detection blocks are supported: R_TRIG and F_TRIG.

R_TRIG - Rising Edge Detector

The R_TRIG standard function block detects a rising edge on the input CLK.

ParameterTypeDirectionDescription
CLKBOOLInputA rising edge sets the output Q for one execution.
QBOOLOutputThe output value.

The Rising Edge Detector is logically equivalent to:

Function_Block R_TRIG
    Var_Input CLK : BOOL; End_Var
    Var_Output Q : BOOL; End_Var
    Var M : BOOL; End_Var

    Q := CLK AND NOT M;
    M := CLK;
End_Function_Block

Using the Rising Edge Detector (R_TRIG)

The following example shows how to conditionally execute code when the tag %"DigitalTag" changes from FALSE to TRUE:

Program MAIN
    Var 
        (* The rising edge detector *)
        DetectEdge : R_TRIG; 

        (* The tag to monitor *)
        TagValue AT %"DigitalTag" : BOOL;
    End_Var

    (* Execute the edge detector function block *)
    DetectEdge( CLK:=TagValue );

    (* Check the output state *)
    IF DetectEdge.Q THEN

        (* Code to execute when %"DigitalTag" transitions from FALSE to TRUE *)

    END_IF;

End_Program

Standards Compliance

Table 35.1 - IEC 61131-3 Second Edition.

Table 44.1 - IEC 61131-3 Third Edition.

F_TRIG - Falling Edge Detector

The F_TRIG standard function block detects a falling edge on the input CLK.

ParameterTypeDirectionDescription
CLKBOOLInputA falling edge sets the output Q for one execution.
QBOOLOutputThe output value.

The Falling Edge Detector is logically equivalent to:

Function_Block F_TRIG
    Var_Input CLK : BOOL; End_Var
    Var_Output Q : BOOL; End_Var
    Var M : BOOL := TRUE; End_Var

    Q := NOT CLK AND NOT M;
    M := NOT CLK;
End_Function_Block

Standards Compliance

Table 35.2 - IEC 61131-3 Second Edition.

Table 44.2 - IEC 61131-3 Third Edition.

Note: The implementation follows the recommendation in IEC 61131-8 Guidelines for the application and implementation of programming languages. See section 3.8.1.2 - Use of edge-triggered function blocks. IEC 61131-8 recommends the CLK input of F_EDGE must be first detected as TRUE before a transition from TRUE to FALSE is detected. This contradicts the IEC 61131-3 standard where the output Q will be TRUE following a cold restart with the CLK input disconnected or FALSE.

Note: If you want F_TRIG to follow the IEC 61131-3 standard override the M variable of the function block:

Var
    (* F_TRIG following the strict interpretation of the IEC 61131-3 standard *)
    DetectEdge : F_TRIG := (M:=FALSE);
End_Var

Further Information

Standard Function Blocks

To learn about other standard function blocks.

Elementary Data Types

To learn about the different types supported by Fernhill SCADA.

Common Elements

To learn about other IEC 61131-3 Common Elements.

Glossary

For the meaning of terms used in Fernhill SCADA.