Main Site Documentation

G120 Interrupt Usage


I have the interrupt pin on a MCP23017 connected to PO.13 on G120

I am using the code below…

            InterruptPort IntButton = new InterruptPort(Pin.P0_13, false,Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);

            IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);

When i start my application…the interrupt is fired when the MCP23017 is initilised but the interrut in fired after the first time. Do i need to clear the interrupt on the G120…i do clear the interrupt on the MCP23017


What does your interrupt handler do ? Show us it’s code… Also, check the datasheet of the MCP23017 and see what it says about clearing interrupts. Alternatively, you could step back to a simpler test, and use a button connected to a pin and see what happens with that to check your code works as you expect…


Thanks Brett…my code is correct in regard to interrupt trigger…must be something with MCP23017 config. mmm…will check my code for MCS23017 setup

        static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
            Debug.Print("Button Pressed");
           byte InputsB = MUX_BTN_OPTO_FETS.ReadPinsB(PortExpander.Pin.ALL);//to clear intrrupt



When an interrupt is triggered…i try to clear it but locks up the device when the clear interrupt is atempted.

        static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
            Debug.Print("Button Pressed InterruptCount[" + InterruptCount++ + "]");

        public byte Read(byte reg)
            singleCommand[0] = (byte)reg;
            readTrans[0] = I2CDevice.CreateWriteTransaction(singleCommand);
            readTrans[1] = I2CDevice.CreateReadTransaction(readBuffer);
            int resylt = _Device.Execute(readTrans, 100);

            if (resylt > 0)
                if (Program.DebugLevel >= 4) Debug.Print(" Read " + reg + "  I2C device[" + _Device.Config.Address + "] Result[" + readBuffer[0] + "]");
              if (Program.DebugLevel>=1)  Debug.Print(" Read " + reg + "  I2C device[" + _Device.Config.Address + "] No data returned");

            return readBuffer[0];


so the problem lies in your communication to the MCP23017 device. Define “lock up”? You have the ability to step into code, tell us where you’re getting to and what the behaviour is? You also mentioned looking at the data on the bus in the past, what is going on at that level?


Page 21, section 1.6.9 explains how the interrupt is cleared. You have to read either the GPIO ot INTCAP registers.

Also, check the NOTE on page 24.


Thanks Dave…i have read those sections and applied the logic but its not working as expected…

   MUX_BTN_OPTO_FETS = new PortExpander.Mcp23017(0x23, null);
            MUX_BTN_OPTO_FETS.PinDirectionB(PortExpander.Pin.ALL, PortExpander.Direction.Input);
            MUX_BTN_OPTO_FETS.PinInterruptControlB(PortExpander.Pin.ALL, PortExpander.InterruptControl.PreviousValue);
            MUX_BTN_OPTO_FETS.PinDefaultValueB(PortExpander.Pin.ALL, false);

            MUX_BTN_OPTO_FETS.PinInterruptOnChangeB(PortExpander.Pin.ALL, true);

            MUX_BTN_OPTO_FETS.PinDirectionA(PortExpander.Pin.ALL, PortExpander.Direction.Input);
            MUX_BTN_OPTO_FETS.PinDefaultValueA(PortExpander.Pin.ALL, false);
            MUX_BTN_OPTO_FETS.PinInterruptControlA(PortExpander.Pin.ALL, PortExpander.InterruptControl.PreviousValue); //Doesn't interupt if is use PortExpander.InterruptControl.DefaultValue
            MUX_BTN_OPTO_FETS.PinInterruptOnChangeA(PortExpander.Pin.ALL, true);

        static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
            if (LogLevel >= LoggingLevel.debug) Debug.Print("Button Pressed InterruptCount[" + InterruptCount++ + "]");
              InputsB = MUX_BTN_OPTO_FETS.ReadPinsB(PortExpander.Pin.ALL);

            InputsA = MUX_BTN_OPTO_FETS.ReadPinsA(PortExpander.Pin.ALL);

    public void Reset()
            if (resetPin != null)
                //set default Reset values if not using hardware reset pin
                //Write(_IODIRA, 0x00);//00=OUTPUT
                Write(_IPOLA, 0x00);
                Write(_GPINTENA, 0x00);//Interupts off
                Write(_DEFVALA, 0x00);
                Write(_INTCONA, 0x00);
                Write(_IOCONA, 0x00);
                Write(_GPPUA, 0x00);
                Write(_INTFA, 0x00);
                Write(_INTCAPA, 0x00);
                Write(_GPIOA, 0x00);
                Write(_OLATA, 0x00);

                //Write(_IODIRB, 0x00);//00=OUTPUT
                Write(_IPOLB, 0x00);
                Write(_GPINTENB, 0x00);//Interupts off
                Write(_DEFVALB, 0x00);
                Write(_INTCONB, 0x00);
                Write(_IOCONB, 0x00);
                Write(_GPPUB, 0x00);
                Write(_INTFB, 0x00);
                Write(_INTCAPB, 0x00);
                Write(_GPIOB, 0x00);
                Write(_OLATB, 0x00);

My board will somtimes feeze if the interrupt get called too often?

Are their any best practices when using interrupts? SHoud i avoid other threads etc?


@ anthonys - If the board gets locked up due to the frequency in which the interrupt is handled, you can try to disable interrupts on the pin at the beginning of processing, and re-enabled at the end, that is, if this kind of behavior is OK with the device as well as the data the device would be sending you.


Thanks James

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