Main Site Documentation

[Answered]Fez Panda II Interrupt---HELP!


#1

I am new to both C# and the micro framework. In the process of using the beginners guide and trying to learn the micro framework I came across the section on interrupts and now I am completely lost! the code from the beginners guide is as follows:

static OutputPort LED;

        public static void Main()
        {
            LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true);
            // the pin will generate interrupt on high and low edges
            InterruptPort IntButton =  new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true,
            Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            // add an interrupt handler to the pin
            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
            //do anything you like here
            Thread.Sleep(Timeout.Infinite);
        }
        static void IntButton_OnInterrupt(uint port, uint state,DateTime time)
        {
            // set LED to the switch state
            LED.Write(state == 0);
        }

1st question:

Where do the values passed to the IntButton_OnInterrupt function come from and how can I change what is passed to the function? ( I want to make an interrupt act as a switch. i.e. press once to turn on and press again to turn off.)

2nd question:

If state is passed to the function why do we have to set it to ‘0’?


#2

1st question:

Values come from MF which sets the values based on the state of the pin and the type of the interrupt.

To make it a switch you have to keep the state of the switch in local variable and change it on every event.

2nd question:

We do not set (=) it, we compare (==) it.

P.S. Use “code” button for proper formatting.


#3

I edited your post to include code tags, hope you do not mind :slight_smile:


#4

Thanks for all your help. This is the final code that I came up with. It’s not the prettiest stuff but it works. Hopefully this will help someone else.


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace FEZ_Panda_II_Interrupts
{    
    public class Program
    {
        // this moved out here so it can be used by other methods       
        static OutputPort LED;
        static bool Write = false;
        
        public static void Main()
        {            
            LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di3, true);
            // the pin will generate interrupt on high and low edges
            InterruptPort IntButton =  new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.Di30, true,
            Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            // add an interrupt handler to the pin            
            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
            //do anything you like here
            Thread.Sleep(Timeout.Infinite);
        }


        static void IntButton_OnInterrupt(uint port, uint state,DateTime time)
        {

            uint Flip = state;
            
            if (Flip == 0)
            {
                Write = !Write;
            }
            
            // set LED to the switch state
            LED.Write(Write);
        }
    }
}