Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Interrupt Port and Debouncing

Debouncing InterruptPorts

  • Please log in to reply
3 replies to this topic

#1 bgreer5050

bgreer5050

    Member

  • Members
  • PipPip
  • 28 posts

Posted 30 March 2015 - 01:45 PM

I think the problem that I am having is being caused by debouncing but I am not sure why I am experiencing it since I believe I have code that should mitigate it.

 

I declare an Interrupt (heartBeatInput):

 

 heartBeatInput = new InterruptPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);

 

 

In my OnInterrupt event I check for the value of data2 to see if the pin is high or low.  I have D1 pulled down to ground with a 4.7k resistor.  I trigger the input by touching D1 to 3.3v and here is where I get confused.  First of all, I do expect a little debouncing, but I a, seeing the Interrup event handled so many times that I believe it is causing am Out of Memory exception that I am seeing.  I expect my heartBeatInput_OnInterrupt to not fire again until I call   heartBeatInput.ClearInterrupt().  I call the ClearInterrupt method in my handleHeartBeat method. But it does fire, so I am checking for the value of data2.  

 

Why is heartBeatInput_OnInterrupt firing again before the ClearInterrupt is called ?  How can I better handle this problem ?

 

 

********************************************************* CODE **********************************************************

      static void heartBeatInput_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            //Debug.GC(true);
            //Debug.EnableGCMessages(true);
            try
            {
 
                Debug.Print(data2.ToString());
                
                if (data2 == 1)
                {
                    bool blnHandle = false;
 
                    TimeSpan ts = time - timeOfLastHeartbeat;
 
                    // TODO Make this a variable that is declared above
                    if (ts.Ticks > 1500000) //Must be greater than 1.5 seconds
                    {
                        blnHandle = true;
                    }
                    else
                    {
                        blnHandle = false;
 
                    }
                    //}
                    if (blnHandle == true)
                    {
                        Debug.Print("Handle = True");
                        handleHeartBeat(time.ToUniversalTime());
                    }
                    else
                    {
                        //Debug.Print("DO NOT HANDLE");
                    }
 
                }
            }
            catch(Exception ex)
            {
                Debug.Print(ex.Message);
                Thread.Sleep(4000);
            }
            //Debug.GC(true);
        }

 

 

 

 

 

 

 

 private static void handleHeartBeat(DateTime time)
        {
            totalRuntimeMilliseconds += getMillisecondsSinceLastHeartBeat(time);
            totalNumberOfCycles++;
            numberOfHeartBeatsSinceLastStateChange++;
            TimeSpan ts = time - timeOfLastHeartbeat;
            double totalMillisecondsSinceLastCycle = ts.Ticks / 10000.0;
            Debug.Print(totalMillisecondsSinceLastCycle.ToString());
 
            if (currentSystemState == SystemState.DOWN &&
            numberOfHeartBeatsSinceLastStateChange > numberOfHeartBeatsRequiredToChangeState &&
            totalMillisecondsSinceLastCycle < (cycleMillisecondsLength * 1.5))
            {
 
                setSystemSateToRun(time);
            }
            timeOfLastHeartbeat = time;
 
            heartBeatInput.ClearInterrupt();
            //Debug.GC(true);
            //Debug.Print("Number of cycles: " + totalNumberOfCycles
            //    + "  Total cycle time: " + totalRuntimeMilliseconds);
        }


#2 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 30 March 2015 - 10:26 PM

From 

https://msdn.microso...y/cc532335.aspx

 

"When configuring an interrupt port, note the differences between level and nonlevel interrupts. Level interrupts, which are either InterruptEdgeLevelHigh or InterruptEdgeLevelLow interrupts, are dispatched when the value on a pin matches the specified high value or low value, respectively. The system dispatches only the first occurrence of a level interrupt until it is cleared by means of the ClearInterrupt method. With nonlevel interrupts, every specified edge is dispatched, and the ClearInterrupt method throws anInvalidOperationException.

In practice, it is best to use level interrupts when the interrupt condition needs to be checked only periodically."

 

Based on the above explanation, your interrupt will ONLY fire when the edge is high, so checking for both high and low states in the interrupt event is not necessary.  I would also think that heartbeatInput.ClearInterrupt() should be called unconditionally from the interrupt event handler itself, before it exits the handler, (since your "handleHeartBeat()" will only be called from time to time.)  

On first look of your code, the ClearInterrupt will not always be fired - it will only be called if ts.Ticks > 1500000.

 

Also - you might try using the GlitchFilter to de-bounce.... I believe that's one of the things it is supposed to be used for.

 

Brian M.



#3 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 31 March 2015 - 12:12 AM

Sorry - I just realized upon re-reading that you are not using Level interrupts.  My bad.  

 

Brian M.



#4 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 10 April 2015 - 05:18 PM

Check this:

 

http://forums.netdui...tedgelevelhigh/






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.