Supported platforms: CODESYS 3.5, CODESYS 3.5 SAFETY

 

S_DebounceDI (FB)

This FB exists in library version 1.1.0.0 or newer.

 

Description

This function block can be used to filter out switch bouncing in digital inputs caused by mechanical contacts in buttons, relays, etc… POU contains two different filtering modes: Debounce and Debounce_Fast.

 

Initialization

Init method is used to initialize the necessary static calculation parameters for the function block instance.

The method shall be called at least once before the actual function block can be called, i.e. at the application initialization phase.

The method checks the validity of input parameters. However, none of the function block outputs are affected by the initialization.

 

A parameter error is diagnosed if:

  • Init method is not called before the main FB call OR

  • i_pEventCode = 0

 

Initialization parameters

Parameter

Type

Unit

Range

Description

S_i_DebounceDelay

SAFEUINT

ms

-

Max assumed bouncing time of the contact device connected to input pin 

i_FilterRype

DebounceFilterType

-

-

The selected debounce filter type. Either Debounce or Debounce_Fast.

i_pEventCode

POINTER TO EventCode

-

≠0

Pointer to application variable which is type EventCode.

 

Initialization method return value

TRUE: All initialization parameters ok.

FALSE: Error in initialization parameter(s).

 

Operation

This FB is in the normal operation mode when the input i_Enable is TRUE and all input parameters are valid. In normal operation mode the FB filters out the DI debouncing and returns filtered DI status. FB contains two different options for filtering: Debounce and Debounce_Fast.

 

DebounceDebounce

 

Above picture demonstrates the voltage behaviour in inputs caused by bouncing of mechanical contact in, for example, button, relays, etc... Purpose of this POU is filter this bouncing out to get stable boolean status.

This shall be achieved by waiting that input has stopped bouncing and the value is stable.

The input state must be stable for a given time before the output state is changed.

The output is activated when the input value has been true continuously for the given time of S_i_DebounceDelay.

The output is deactivated when the input value has been false continuously for the given time of  S_i_DebounceDelay.

 

Debounce_FastDebounce_Fast

 

Above picture demonstrates the voltage behaviour in inputs caused by bouncing of mechanical contact in, for example, button, relays, etc... Purpose of this POU is this filter this bouncing out to get stable Boolean status.

This shall be achieved by limiting how frequently output state can change.

Output state changes equal to input on the rising or falling edge of the change. After this there is an adjustable delay (S_i_DebounceDelay) in which time the output state is not changed regardless of the input state. After the delay, the cycle starts again. So, after each change of output there is a time delay before the output can be changed again.

 

Inputs

Input variable name

Data type

Range

Description

i_Enable

BOOL

TRUE / FALSE

Enable conversion

S_i_DigitalState

SAFEBOOL

TRUE / FALSE

DI status without debounce

Outputs

Output variable name

Data type

Range

Description

S_o_DigitalState

SAFEBOOL

TRUE / FALSE

De-bounced DI status

o_Status

Status

-

Status of output value. See Status structure.

 

Error diagnostic

See Diagnostic Interface library description of error status and event code functionality.

 

Conditions

S_o_Digital State

o_Status. OutputValid

o_Status error status

Event code FunctionID

Event code EventID

i_Enable TRUE

AND

init parameter error AND
i_pEventCode NULL

FALSE

FALSE

ParameterError

-

-

i_Enable rises

AND

 init parameters ok

de-bounced DI status

TRUE

-

NO_FUNC

NO_ERROR

 

 

Example code

MultiTool Creator generated code includes this function block, when the input is selected to be a DI.

 

Location of the POU instance depends on the application: Non-safe inputs are handled in non-safe context, safe inputs are handled in safe context. The example code applies to both.

 

Definitions:

 

(*NOTE! This object is created by Epec MultiTool Creator*)

 

VAR_INPUT

    i_Enable:  BOOL := TRUE;

    i_DI_XM1_03:BOOL;                // Input DI value

END_VAR

VAR_OUTPUT

    S_o_DI_XM1_03: SAFEBOOL;    

    o_XM1_03_Status_DebounceDI: EPEC_SC.Status;

    o_EC_XM1_03: EPEC_DITF.EventCode;

END_VAR

VAR

    init_ok: BOOL := FALSE;    

    S_1_03_DebounceDI: EPEC_SC.S_DebounceDI;

   

    S_XM1_03_DebounceDelay: SAFEUINT := UINT#30;   // ms

END_VAR

 

 

 

 

Init:

 

 

(*NOTE! This object is created by Epec MultiTool Creator*)

 

o_EC_XM1_03.DeviceID := DeviceIDs.DigitalInput;

o_EC_XM1_03.ChannelID := ChannelIDs.Ch1;

o_EC_XM1_03.FunctionID := EPEC_DITF.FunctionIDs.NO_FUNC;

o_EC_XM1_03.EventID := EPEC_DITF.ErrorIDs.NO_ERROR;

 

init_ok := S_1_03_DebounceDI.Init(

    S_i_DebounceDelay:=S_XM1_03_DebounceDelay,

    i_FilterType:=EPEC_SC.DebounceFilterType.Debounce_Fast, 

    i_pEventCode:=ADR(o_EC_XM1_03)

);

 

 

Code:

 

 

(*NOTE! This object is created by Epec MultiTool Creator*)

 

S_1_03_DebounceDI(

    i_Enable:=i_Enable,

    S_i_DigitalState:=i_DI_XM1_03,

    o_Status => o_XM1_03_Status_DebounceDI,

    S_o_DigitalState => S_o_DI_XM1_03

);

 

 

See also

 

 

 

 

Source file Topic100629.htm

Last updated 13-Jun-2024