This is an old revision of the document!


FIXME - překlad

Digital inputs: debounce, counter and direct switch

The digital inputs on UniPi controllers have interesting “add-ons”, which help accuracy and responsiveness of the regulation. These add-ons are configured in the CPU of the input/output backplane board in UniPi, so they are SW independent and therefor very fast.

The debounce functionality is for catching glitches which typically occur when changing states on digital input. Those happen mostly when the digital input is operated by some mechanical element, e.g. momentary switch. The signal generted upon pressing and releasing a momentary switch can look like this:

To “debounce” these glitches, you have to slow down the reaction on changing states of the input. On HW level, you could achieve this with employing a RC element for example. To make your life easier, you can set this “slowing down” on SW level.

The functionality looks for changes on a digital input. If the change is less then defined debounce time, it will be ignored. Once the change exceeds the debounce time, then the event is passed to the Mervis.

By default, all digital inputs are equipped with 5ms debounce time

Let's see, how the debounce can be configured in Mervis. The configuration is done per device input, so double click on the UniPi's controller name. In the Main window, the tab with all controller's registers will apear. Under the Name, you have a search box, where by entering “DI_” you will be presented with the list of digital inputs, or more precisely, their registers.

Select the desired digital input, for which you want to set the debouncing. In the Properties panel you can see all the options you can set on this register. The option we are interested in right now is the Debounce. Value of this option is number of miliseconds of deboucing period.

We just changed the configuration of the PLC modules, which is not applicable by simply uploading the solution. To save the configuration, follow the UniPi - Configure Module tutorial.

If you are about to set options on multiple registers, you can do it at once and Configure module afterwards

Another interesting functionality of digital inputs is the counter. It is a software independent, high-speed counter of rising edges of changing signal on digital input. In simple words, it can count short pulses very precisely.

The speed of Mervis programs is not enough for detecting quick changes of digital inputs in traditional way (with the “Pulse counter (BD30)” FUPLA box for example). If you have a slot car track and you want to count the number of passes of slot cars, you wouldn't be able to detect it, since the length of pulses from the “pass” sensors will be in units of miliseconds. Speed of the slot car is up to 80kmph and with length of the car about 10cm is the pass of whole car above the sensor about 4.5ms.

The HW counter depends on the Debounce settings. Therefor the Debounce has to be set to lower value, than is the expected length of the pulses to count.

Each digital input is equipped with counter and you don't need to set anything to use it. The counter is available through global variable. For the input named Neuron_L503_DI_1.01 we have counter variable named Neuron_L503_CNT_1.01.

To demonstrate how slow is the software counting, look at this program in debugging mode:

As you can see, we are counting the rising edge events on the Neuron_L503_DI_1.01 through Pulse counter (BD30) function block, and reading the HW counter of the same input via the Neuron_L503_CNT_1.01 variable. Afterwards, we compare those two variables in eq function block, and if they are equal, we will turn the X1 LED on the controller ON. Once the two counters are not equal, the LED will be OFF.

The debugging started with both counter values at 0. We connected a momentary switch to the DI1.1 input and start pressing the switch very quickly. As you can see, the software counter detected only 10 presses, but the hardware counter detected 54.

The counter variables are read only, therefor you cannot set or reset them directly from program or the variable browser in debugging mode. You can set or reset the variables via “set” variable. For the Neuron_L503_CNT_1.01 the set variable is called Neuron_L503_CNT_1.01_set.

Last extra functionality (but not least) is the direct switch. As we discovered, the software detection of changes is far from being instantaneous. And there are applications, where even 20ms delay can cause problems, or represent some discomfort at least. Typical example is a switch controlling a light. What we expect from the behaviour is the instant change of the lighting upon pressing the switch. That, as demonstrated above, is not possible to achieve in the software by simple wiring the DI to DO.

The direct switch is the configuration of digital input in a way it will directly control digital or relay output of the same name. The wiring of the input and output is fixed a cannot be changed, only turned on or off. On Group 1, the DIs are wired to DOs. On other Groups, the DIs are wired to ROs.

On Neuron L503, the direct switch (DS) on DI1.1 is wired to DO1.1. The DS on DI3.14 is wired to RO3.14

The settings is done in the same way we did with the Debounce. Double click on the name of the UniPi module on the Left panel, search for the desired DI in the Main window, click on it and in the Properties panel look for the option called Direct switch.

As you can see, the option has dropdown menu with different modes.

  • Disabled: the direct switch is disabled
  • Copy: The status of the DI is copied to the DO/RO. So when the DI is TRUE, the DO is TRUE and when the DI is FALSE the DO is also FALSE
  • Copy (Invert): Same as above but inverted. When the DI is TRUE the DO is FALSE and vice versa.
  • Toggle: This mode toggles the DO on the rising edge of the DI. Handy for controlling a light with momentary switch for example

After setting the configuration, you need tu download it to the module. See the debounce section.

The Direct switch depends on the Debounce settings. In order to have instant change, you need to set the Debounce to value which doesn't deny the purpose of the Direct switch and yet to value, which doesn't deny the purpose of the Debouncing. The 5-10ms should be OK.