Hello. I am new to electronics and programming.
I have been playing with some MCP23017 examples and I have gotten them working.
I would like to try and do my own thing with them now.
Is it possible to get the pin that changed?
What I would like is for a single MCP23017 to be configured for all inputs. When a pin changes I would like to fire a interrupt event that prints which pin changed and its current state.
All the examples I have seen just poll all of the pins. The order of the changes is important for me. So I'd like to know which pin made the interrupt happen.
Hopefully this is possible?
Thanks,
Chris
The INTERRUPT FLAG register has the bit that caused the interrupt set and the INTERRUPT CAPTURE register holds the value of the port at the time of the interrupt.
groundFungus:
The INTERRUPT FLAG register has the bit that caused the interrupt set and the INTERRUPT CAPTURE register holds the value of the port at the time of the interrupt.
Thanks for the reply
I have been playing with it more today.
Trying to strip it right down and understand what is going on.
This is what I have
#include <Wire.h>
// MCP23017 registers.
// Everything except direction defaults to 0
// A
#define IODIRA 0x00 // IO direction (0 = output, 1 = input (Default))
#define IPOLA 0x02 // IO polarity (0 = normal, 1 = inverse)
#define GPINTENA 0x04 // Interrupt on change (0 = disable, 1 = enable)
#define DEFVALA 0x06 // Default comparison for interrupt on change (interrupts on opposite)
#define INTCONA 0x08 // Interrupt control (0 = interrupt on change from previous, 1 = interrupt on change from DEFVAL)
#define IOCON 0x0A // IO Configuration: bank/mirror/seqop/disslw/haen/odr/intpol/notimp
#define GPPUA 0x0C // Pull-up resistor (0 = disabled, 1 = enabled)
#define INTFA 0x0E // Interrupt flag (read only) : (0 = no interrupt, 1 = pin caused interrupt)
#define INTCAPA 0x10 // Interrupt capture (read only) : value of GPIO at time of last interrupt
#define GPIOA 0x12 // Port value. Write to change, read to obtain value
#define OLATA 0x14 // Output latch. Write to latch output.
// B
#define IODIRB 0x01
#define IPOLB 0x03
#define GPINTENB 0x05
#define DEFVALB 0x07
#define INTCONB 0x09
#define GPPUB 0x0D
#define INTFB 0x0F
#define INTCAPB 0x11
#define GPIOB 0x13
#define OLATB 0x15
// chip addresses
#define chip1 0x20 // MCP23017 1 adress
#define chip2 0x21 // MCP23017 2 adress
void setup()
{
// start i2c
Wire.begin();
// start serial
Serial.begin(9600);
// mirror interrupt pins
Wire.beginTransmission (chip1);
Wire.write (IOCON);
Wire.write (0b01100000); // port A
Wire.write (0b01100000); // port B
Wire.endTransmission ();
// enable interrupts
Wire.beginTransmission (chip1);
Wire.write (GPINTENA);
Wire.write (0xFF); // port A
Wire.write (0xFF); // port B
Wire.endTransmission ();
// enable pull ups
Wire.beginTransmission (chip1);
Wire.write (GPPUA);
Wire.write (0xFF); // port A
Wire.write (0xFF); // port B
Wire.endTransmission ();
// read from interrupt capture port to clear - A
Wire.beginTransmission (chip1);
Wire.write (INTCAPA);
Wire.endTransmission ();
Wire.requestFrom (chip1, 1);
// read from interrupt capture port to clear - B
Wire.beginTransmission (chip1);
Wire.write (INTCAPB);
Wire.endTransmission ();
Wire.requestFrom (chip1, 1);
// pin 19 of MCP23017 is plugged into D2 of the Arduino which is interrupt 0
attachInterrupt(0, keypress, CHANGE);
}
void keypress ()
{
Serial.println("Hello World!");
}
void loop()
{
}
I don't understand why "0b01100000" sets the interrupts to mirror, but I think I understand the rest.
It has issues though.
"Hello World!" prints on boot for some reason and I can only get the interrupt to fire once.
Still not entirely sure how to get the pin that changed.
Am I close?
// read pin that caused interrupt
Wire.beginTransmission (chip1);
Wire.write (INTCAPA);
Wire.endTransmission ();
Wire.requestFrom (chip1, 1);
Serial.println (Wire.read());
Thanks,
Chris