Author Topic: Debounce Interupt ??  (Read 18628 times)

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Debounce Interupt ??
« on: June 27, 2010, 11:20:03 AM »
I have a water meter with a dry contact reed switch that cycles 1 time per gallon, and 20 GPM maximum, so maximum frequency that the PLC interrupt will see is less than 2 seconds per cycle. In testing the programming, I have used a jumper wire to generate activity on the interrupt pin, and get multiple interrupts even when trying to minimize.

How can I debounce the interrupt??

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Debounce Interupt ??
« Reply #1 on: June 27, 2010, 03:53:52 PM »
I have a couple suggestions on signal debounce:
  • Use a normal input pin and ladder logic.  When used this way, your input signal is debounced by the PLC's firmware/hardware.  You get this for free.  The frequency of your input is not fast enough to necessitate the use of an interrupt routine.  Relay logic will get the job done, easily.
  • Externally filter your switch by adding a 100 Ohm resistor in series with the input pin and add a 10 uF 35VDC capacitor from the input pin to ground.  The capacitor works in conjunction with the pull-up resistor that is part of the PLC input structure. The 100 Ohm resistor is used to limit the discharge current from the capacitor to a level that will not weld your external switch shut.  If you go down this road, be aware that this is a "kludgey" fix for a problem that you don't need to fix...
Good Luck,

Gary D.

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Re:Debounce Interupt ??
« Reply #2 on: June 27, 2010, 05:28:03 PM »
Thanks for the reply.

I question whether the scan time is short enough to reliably catch the switch pulse. The program is 4200 code words long with many custom functions, some which do have brief waits. I use a counter to select a MD-HMI screen from a half dozen, using a function key to increment the counter. Clicking the function key doesn't reliably increment the counter.

I was thinking there was a method using a timer, but that likely would involve the scan time.

I was thinking, I might have to use a 555 timer to debounce, but if that's  a way to go, probably would just build up a PIC board. Probably got to do that anyway to interface a remote display. This system has no other involvement with the PLC. The PLC has plenty of unused I/O, and could handle this with minimum effect on other duties.

Good thought on the resistor to protect the switch. The switch does have a maximum current limit.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Debounce Interupt ??
« Reply #3 on: June 27, 2010, 11:36:01 PM »
cnenk,

If your real problem is that your scan time is so long that the PLC misses input events, then your problem isn't a debounce issue but a software architecture issue.

I assume that the 555 idea is to act as a "pulse stretcher" to ensure that the PLC will "see" the input.   This helps mask your long scan time problem.

If you go the 555 route, keep in mind the following:
  • The 555, typically, can't tolerate 24V.  These gizmos are pretty happy running between 5 and 15V.
  • The output of the 555 running at 12V can't be connected directly to the PLC input if the PLC is running at 24V.  You'll need to use something on the order of a small transistor or a section of a ULN2003 for level translation.
  • The 555 Trigger pin is very high impedance input.  Be very careful with your wiring to minimize the potential for false triggering.
  • Ensure that some minimum current runs through your external contacts.  20 mA is a good value to ensure that you don't get into dry contact issues.  This also helps with noise issues.
An Aside:

If your scan time has been extended to the point that it misses inputs, its time to rethink your software architecture.  It'd be a good time to instrument your PLC and determine what is actually responsible for your long scan times.

You mentioned that some of your custom functions have "brief waits".  I'm assuming that you are either using the TBASIC DELAY statement or some other time wasting mechanism. I'd start by reworking any custom function that requires a time waster (scan time extender).

Gary D.

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Re:Debounce Interupt ??
« Reply #4 on: June 28, 2010, 06:28:02 AM »
There are no "Delay" used, but there are quite a few loops that take time to execute, including some quick occuring event to occur. I have tried to program to minimize the time, but as a hobbyist, with this being the my first experience with a PLC, I am far from an expert. :( But am always happy to learn. :)

I tried once to determine the scan time by starting a timer at the beginning and reading it next time through the scan, but it didn't seem to provide usable results. Might have to get into that again.

The PLC manages a standby generator that runs 3 fuels (natural gas from our well, gasoline from a nearby 175 gallon tank used for vehicles, or propane) switching fuels automatically depending what is available. The PLC has done this reliably for 5 years.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Debounce Interupt ??
« Reply #5 on: June 28, 2010, 10:03:33 AM »
cdenk,

I done some more thinking on how you might use the interrupt input with a mechanically noisy input.  

I'd suggest adding a bit of ladder logic to mask the bounces and a couple lines to your interrupt code so that it only counts the first bounce as being important.

In the following example Mask is a RELAY used to communicate between your ladder logic and interrupt routine.  Timer1 used to mask the entire time that the contact is ON including all of the bounces.  I'd suggest programming the SV to 10 (1 second) to mask any bounces of both the closing and the opening of the contact.  EnableInts is a NEW custom function to re-enable the interrupt logic once the contacts are known to be no longer bouncing.

OK, first the ladder logic:

  Mask                      Timer1
---||------------------(TIM)

  Timer1                     Mask
---||--------------+---[Clear]
                           |
                           |     EnableInts
                           +---{dCusF}

                       
Now the change to the interrupt code:

INTROFF 1      ' Disable interrupts
SetIO MASK   ' start the event mask timer
'
' the rest of the interrupt code goes here.

New custom functions EnableInts:

INTRDEF 1, fn_num, 1


Give this a try.

Gary D.

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Re:Debounce Interupt ??
« Reply #6 on: June 30, 2010, 06:28:40 PM »
Sorry for not getting back sooner, we had some heavy weather pass through, took down a couple of good sized Norway spruce, have been cleaning up.

Added a Cust Fun near the beginning of the ladder to determine the lapsed time for 1000 cycles through the ladder. Found 1783 maximum HS timer ticks of 0.01 seconds = 0.0178 seconds per cycle. This should easily grab my 2 second period for switch closures. This is in line with Gary's first comment. Hope my findings are correct. Here is the Cust Fun code:

'DM[3945] Is a counter for how many times we have been through the ladder loop
'First time here, start the timer at 6000
if DM[3945] = 0
TimerPV[1] = 6000

ENDIF

IF DM[3945] = 1000
'Timer 1 is set as a high speed timer with 01 second per tick, and will measure the period for the number of iterations
DM[3946] = 6000 - TimerPV[1]
'DM[3946] Is the actual number of time ticks elapsed, and DM[3947} is the maximum number of ticks
IF DM[3946] > DM[3947]
DM[3947] = DM[3946]
 DM[3945] = 0
ENDIF
ELSE
DM[3945] = DM[3945] + 1
ENDIF

I will save the code that Gary generated, probably will be a learning experience.

Thanks again. :)

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Debounce Interupt ??
« Reply #7 on: June 30, 2010, 09:31:59 PM »
cdenk,

You've done well with your instrumentation.  The high resolution timer is an excellent trick.   Tracking the maximum time was even better.

I often wiggle a PLC output bit using a single bit of ladder logic. I'll monitor the output with an old school oscilloscope.  Output16 will change state on every scan:

  Output16                                     Output16
---|\|-------------------------------(OUT)

With your calculation of a 17.8 ms scan period, your program doesn't seem to have put a huge burden on the PLC.  This translates to a scan frequency of over 56 Hz.  This should be plenty fast enough to not miss your contact closure.

I would think that you shouldn't need to use the interrupt mechanism to deal with your water meter.   A normal input pin should work fine.

Well done,

Gary d.

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Re:Debounce Interupt ??
« Reply #8 on: July 01, 2010, 05:25:07 AM »
Thanks again, I'll have to try that output toggle with a DMM that has frequency function. I don't have an o-scope :(

I just thought, based on an issue I have with using one of the HMI function keys to advance a counter to change the HMI display not being reliable was a result of the PLC being slow. But that's an issue for another topic. Probably will be a week or so before that come to the top of the list. Now I can install the meter over the weekend. :)

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Re:Debounce Interupt ??
« Reply #9 on: July 03, 2010, 03:16:10 PM »
2 Items:
1: Toggled an output bit with a rung of logic. Put my DMM in frequency mode on it, and saw around 50hz. but since it takes to scans to make 1 cycle of the pin, that would put the scan at 100 hz. That's all order of magnitude close.
2: Moved the switch input to an ordinary input pin. Had to put a 0.5 second clock special bit in series to debounce. Used an unmounted reed switch with small hand held magnet as a trigger. Looks like that will work. The meter with the reed switch does have a mechanical dial that can be read to the gallon. Likely they have a magnet on one of the dials that turns around once per gallon. The meter is close to the PLC so I can keep an eye on the readings.

Thanks again. :)