Main Site Documentation

Using an InterruptPort and "State" in the handler


#1

(this is a flow on from discussions on InterrruptEdgeModeBoth)

When using an InterruptPort with a Native Event Handler, when using Port.InterruptMode.InterruptEdgeBoth, the information about the “state” of the interrupt does not reflect a particular state. Should it?

Test rig: Panda with 4.1.7.0, Di13 connected to a magnetic reed switch, other side of switch to GND, powered by USB.

App:


        static long Mycount = 0;
        public static void Main()
        {
            // Blink board LED

            bool ledState = false;

            OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
            InterruptPort DoorSensor = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.Di13, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            DoorSensor.OnInterrupt += new NativeEventHandler(DoorSensor_Capture);

            while (true)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(500);

                // toggle LED state
                ledState = !ledState;
                led.Write(ledState);
            }
        }
        static void DoorSensor_Capture(uint port, uint state, DateTime time)
        {
            Mycount++;
            Debug.Print(Mycount+": " + state + " " + time + "." + time.Millisecond + "; " + time.Ticks );
        }

On closing the switch:
Debug output often shows 3 or 4 passes of the interrupt on what should be one “trigger” when I close the switch by bringing the magnet close to it (some of that could be considerd noise, accepted). But most often, all of the passes show a state of 1; only infrequently will one record a 0. The milliseconds between individual readings is always close as well - well within 2 msec.

On opening the switch:
Almost always reliably there is only one event pass, and always reports a state of 1.

Here’s a sample output. Annotations at the end are mine

so there certainly isn’t a pattern - sometimes when 4 interrupts on close register, one has a zero “status” and othertimes it won’t; but in that sample I haven’t found one that gave a zero when only 3 interrupts were registered on closing the switch.

Any thoughts?


#2

A reed switch bounces, thats a fact. So either debounce the switch in hardware, or do it in software. Also the supplied voltage is important (i read that somewhere), and surely 3.3V is too low.

I walked away from using reed switches, and replaced them with hall effect sensors. Allegro is a good source (not at home right now, can’t remember the part number).

I use them with InterruptEdgeBoth and can’t say I have any problems.

Greetings,
Eric


#3

Our driver doesn’t handle denouncing when using edge both. The fix is easy, read the pin state in the interrupt handler and ignore the passed in value.


#4

Indeed as Gus said debouncing is off on edge both.
Hook a cap for filtering. And I would try big external pullup rather than built in micro.


#5

I noticed the same when creating the SD card detection class.
Fix is like Gus says:


private void sdDetectInterrupt_OnInterrupt(uint port, uint state, DateTime time)
{
    // Read input again since the state argument seems unreliable
    if (interrupt.Read())


#6

reed switch and bounce - as I said, I expect some bounce, that is fine.

What I didn’t expect was the STATE to come in the way it did. I expected a lot more with a state of zero; the fact that that occurs very infrequently is just, well, weird.

The issue exists in any use of interrupt though, use two bare wires plugged into the board, when you connect them you’ll see this. I’m surprised there hasn’t been more questions about the original driver for the push button e-block because it uses the state passed in to detect pressed or not-pressed scenarios, which therefore shouldn’t work.

If we’re doing a re-read elsewhere (SD detect) then I can handle that. Still seems like an issue worth addressing in my view.


#7

Eric, you recommended using Hall Effect sensors instead reed switches. That sounds like a great idea.

I want to use these to detect doors or windows opening/closing so I need to use magnets with the sensors. Have you used these with magnets? Can you recommend a suitable magnet to use?

Thanks


#8

These are the hall effect sensors i use: Allegro A3214
Magnets, whatever you want, the sensor is bi-polar. Did you know that in Hard disks there are tiny magnets that are of great use for this kind of application?


#9

Thanks Eric I’ll check this out :slight_smile:


#10

OK, so a few mods:

Move static declaration of InterruptPort outside main;
Local variable in handler, and read & display interrupt value.

Output now totally makes sense:

[quote]1: 0 01/01/2009 00:14:38.805; 128752424788057762
2: 0 01/01/2009 00:14:38.805; 128752424788058965
3: 0 01/01/2009 00:14:38.806; 128752424788060217
4: 1 01/01/2009 00:15:10.722; 128752425107224748
5: 0 01/01/2009 00:15:21.156; 128752425211568081
6: 0 01/01/2009 00:15:21.156; 128752425211569283
7: 0 01/01/2009 00:15:21.157; 128752425211570595
8: 0 01/01/2009 00:15:21.157; 128752425211571823
9: 1 01/01/2009 00:15:44.105; 128752425441056502
10: 0 01/01/2009 00:15:57.167; 128752425571675263
11: 0 01/01/2009 00:15:57.167; 128752425571676466
12: 0 01/01/2009 00:15:57.167; 128752425571677888
13: 0 01/01/2009 00:15:57.167; 128752425571679082
14: 1 01/01/2009 00:15:58.306; 128752425583066748
15: 0 01/01/2009 00:16:00.146; 128752425601469430
16: 0 01/01/2009 00:16:00.147; 128752425601470691
17: 0 01/01/2009 00:16:00.147; 128752425601471884
18: 0 01/01/2009 00:16:00.147; 128752425601473077
19: 1 01/01/2009 00:16:00.988; 128752425609889025
20: 0 01/01/2009 00:16:03.380; 128752425633804522
21: 0 01/01/2009 00:16:03.380; 128752425633805774
22: 0 01/01/2009 00:16:03.380; 128752425633807259
23: 0 01/01/2009 00:16:03.380; 128752425633808452
24: 1 01/01/2009 00:16:03.380; 128752425633809883
25: 1 01/01/2009 00:16:03.435; 128752425634359570
26: 0 01/01/2009 00:16:04.687; 128752425646872136
27: 0 01/01/2009 00:16:04.687; 128752425646873338
28: 0 01/01/2009 00:16:04.687; 128752425646874532
29: 0 01/01/2009 00:16:04.687; 128752425646876428
30: 1 01/01/2009 00:16:07.893; 128752425678930624
31: 0 01/01/2009 00:16:09.78; 128752425690783668
32: 0 01/01/2009 00:16:09.78; 128752425690784871
33: 0 01/01/2009 00:16:09.78; 128752425690786064
34: 1 01/01/2009 00:16:10.433; 128752425704335822
35: 0 01/01/2009 00:16:11.539; 128752425715396157
36: 0 01/01/2009 00:16:11.539; 128752425715397360
37: 0 01/01/2009 00:16:11.539; 128752425715399258
38: 1 01/01/2009 00:16:12.943; 128752425729436453
39: 0 01/01/2009 00:16:14.88; 128752425740880837
40: 0 01/01/2009 00:16:14.88; 128752425740882112
41: 0 01/01/2009 00:16:14.88; 128752425740883306
42: 1 01/01/2009 00:16:14.259; 128752425742596763
43: 0 01/01/2009 00:16:14.468; 128752425744680124
44: 0 01/01/2009 00:16:14.468; 128752425744681327
45: 0 01/01/2009 00:16:14.468; 128752425744682520
46: 0 01/01/2009 00:16:14.468; 128752425744683713
47: 1 01/01/2009 00:16:18.196; 128752425781963287[/quote]