I have a spring loaded push button that i connected to pin 11 AND the other side to GND. I’m using the following code to register an eventhandler:
private HardwareButton ResBtn;
private HardwareButton StartBtn;
ResBtn = new HardwareButton(FEZ_Pin.Interrupt.LDR);
ResBtn.ButtonPressEvent += new HardwareButton.ButtonPressEventHandler(Interupt_ButtonPressEvent);
StartBtn = new HardwareButton(FEZ_Pin.Interrupt.Di11);
StartBtn.ButtonPressEvent += new HardwareButton.ButtonPressEventHandler(Interupt_ButtonPressEvent);
When I test this code using the loader (LDR) button i have no problems at all. But when i connect another it doesn’t work. The debugger shows that in the following segment of code the data2 value ==1 instead of 0.
void OnInterrupt(uint data1, uint data2, DateTime time)
{
ButtonState state = (data2 == 0) ? ButtonState.Pressed : ButtonState.NotPressed;
ButtonPressEvent((FEZ_Pin.Interrupt)data1, state);
InterruptPort p = button as InterruptPort;
if (p != null)
{
p.ClearInterrupt();
}
}
And to be honest i have no idea why. I also played with the glitchfilter settings, googled around the internet but i’ve got no idea why this does not work as expected
using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.FEZ;
using Microsoft.SPOT.Hardware;
namespace FexPandaTimerTest
{
class HardwareButton : IDisposable
{
private InputPort button;
public delegate void ButtonPressEventHandler(FEZ_Pin.Interrupt pin, ButtonState state);
public event ButtonPressEventHandler ButtonPressEvent = delegate { };
public HardwareButton(FEZ_Pin.Interrupt pin)
{
button = new InterruptPort((Cpu.Pin)pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
button.OnInterrupt += new NativeEventHandler(OnInterrupt);
}
public HardwareButton(FEZ_Pin.Digital pin)
{
button = new InputPort((Cpu.Pin)pin, true, Port.ResistorMode.PullUp);
}
void OnInterrupt(uint data1, uint data2, DateTime time)
{
ButtonState state = (data2 == 0) ? ButtonState.Pressed : ButtonState.NotPressed;
ButtonPressEvent((FEZ_Pin.Interrupt)data1, state);
InterruptPort p = button as InterruptPort;
if (p != null)
{
p.ClearInterrupt();
}
}
public ButtonState GetState()
{
return (button.Read() == false) ? ButtonState.Pressed : ButtonState.NotPressed;
}
#region IDisposable Members
public void Dispose()
{
button.Dispose();
}
#endregion
}
}
Above is my class HardwareButton but it is exactly the same as mentioned in the link in my first post. I tried the example and didnt have any problems except when using an external button.
public class Program
{
private static InterruptPort button;
private static InterruptPort button2;
public static void Main()
{
button = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.Di11, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
button2 = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
button2.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
}
static void button_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (data2 == 0)
{
Debug.Print("Button pressed" + data1);
}
}
}
If i press the button attached to Di11 then the data2 ==1 instead of zero. The other end of the pushbutton is connected to the GND pin. If I press the loader button then data2 will be zero and it will show a message that the button is pressed.
I believe glitch filter with InterruptEdgeBoth interrupts are not supported.
Please either try a high edge or a Low edge. If you need both edges, you have to disable glitch filter.
We will update the ebook.
Well i guess its probably got something to do with my electronic skills instead of my program skills. Because the onboard buttons do work. But i thought they weren’t connected in another way then my external button is.
Gus, it seems that indeed it had to do with my wiring of the whole thing. Also i changed the Port.InterruptMode.InterruptEdgeBoth to EdgeHigh and it worked. Thanks for your time.