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

How to Handle Multiple Interrupts


  • Please log in to reply
6 replies to this topic

#1 rubxcubedude

rubxcubedude

    New Member

  • Members
  • Pip
  • 7 posts

Posted 30 May 2012 - 05:28 PM

I'm currently building off the sample webserver project. I've set up to pins with interrupts. I call the ClearInterrupt function at the end of the interrupt. However, the interrupt continuously runs. Is there a way where i can have an interrupt perform an action and then leave the interrupt and not run it again until the state of that port changes?

namespace NetduinoPlusApplication1
{

        public class Program
        {
            // initialize the onboard LED
            private static OutputPort LEDDisplay = new OutputPort(Pins.GPIO_PIN_D1, false);
            private static InterruptPort switchPort = new InterruptPort(Pins.GPIO_PIN_D2, false,
                                                             Port.ResistorMode.Disabled,
                                                             Port.InterruptMode.InterruptEdgeBoth);
            private static InterruptPort switchPort2 = new InterruptPort(Pins.GPIO_PIN_D3, true,
                                                             Port.ResistorMode.Disabled,
                                                             Port.InterruptMode.InterruptEdgeBoth);
            public static void Main()
            {
                // initialize the onboard switch
                // add an event-handler for the switch
                switchPort.OnInterrupt += new NativeEventHandler(switchPort_OnInterrupt);
                switchPort2.OnInterrupt += new NativeEventHandler(switchPort2_OnInterrupt);
                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp();
                WebServer webServer = new WebServer();
                webServer.ListenForRequest();
            }

            ///
            /// Event handler for the onboard switch.
            ///
            ///The port for which the event occurs.
            ///The state of the switch.
            ///Time of the event.
            private static void switchPort_OnInterrupt(uint port, uint data, DateTime time)
            {
                // turn the LED on, if the button is pressed (== 0, because switch is inverted)
                Debug.Print("Port: " + port.ToString() + " State: " + data.ToString());
                Thread.Sleep(1500);
                switchPort.ClearInterrupt();
            }
            private static void switchPort2_OnInterrupt(uint port, uint data, DateTime time)
            {
                // turn the LED on, if the button is pressed (== 0, because switch is inverted)
                Debug.Print("Port2: " + port.ToString() + " State2: " + data.ToString());
                Thread.Sleep(3000);
                switchPort2.ClearInterrupt();
            }
        }
}


#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 31 May 2012 - 09:29 AM

From your code comments it seems the buttons are switched to ground - do you have pull-up resistors connected (so the port is in defined state when the button is not switched on)? If not, I would try using ResistorMode.PullUp to enable Netduino's internal pull-up resistors. Also, you may consider adding a capacitor connected between the switch contacts to reduce bouncing (tens to hundreds of nF are commonly used, the exact value depends on the button bouncing characteristics). It should not be necessary to call ClearInterrupt(), this method is intended for level interrupts, not edge.

#3 rubxcubedude

rubxcubedude

    New Member

  • Members
  • Pip
  • 7 posts

Posted 05 June 2012 - 01:25 PM

From your code comments it seems the buttons are switched to ground - do you have pull-up resistors connected (so the port is in defined state when the button is not switched on)? If not, I would try using ResistorMode.PullUp to enable Netduino's internal pull-up resistors. Also, you may consider adding a capacitor connected between the switch contacts to reduce bouncing (tens to hundreds of nF are commonly used, the exact value depends on the button bouncing characteristics).

It should not be necessary to call ClearInterrupt(), this method is intended for level interrupts, not edge.


I changed my resistormode to pullup as you suggested and my interrupts do not trigger at all now. I've tried triggering them with the 5V jumper from the Netduino itself. It seems something is stopping my interrupts from triggering correctly.

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 June 2012 - 01:33 PM

When pull-up resistors are used you need to connect the pin to ground to trigger the interrupt.

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 June 2012 - 01:39 PM

Also, if you make the connection by hand, don't be surprised with the erratic behavior - a properly debounced switch will give much better results.

#6 rubxcubedude

rubxcubedude

    New Member

  • Members
  • Pip
  • 7 posts

Posted 05 June 2012 - 02:13 PM

When pull-up resistors are used you need to connect the pin to ground to trigger the interrupt.


i wish to trigger my interrupt on high voltages(aka 5 volts) though. is there a way to do such?

#7 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 June 2012 - 02:23 PM

Yep. However, Netduino does not have built-in pull-down resistors, so you'd need to use ResistorMode.Disabled and add an external one: connect ~10 - 100 kΩ resistor between Netduino pin (e.g. D2) and ground, then you can use positive voltage (~3 - 5V) to trigger the interrupt.




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.