Hi,
I have problems referring to the MCP23017 with the Adafruit-library with the Arduino pro Micro/Leonardo and Interrupts. What I already tried: The I2C-communication works fine, reading the push-button and connecting the push button directly to the interrupt pin D7 on the Arduino works. This is only a small part of a project, therefore I need the MCP.
The problem is that the Interrupt does nothing. Only if I read the push-button pin manually in the main loop the interrupt service routine gets activated. I'll send a schematic and the Code.
Could anyone give me a hint?
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#define TASTER 7 //push button
#define INTERPIN 7 //Interrupt pin Arduino pro Micro
Adafruit_MCP23017 mcp;
volatile boolean awakenByInterrupt = false;
void setup() {
mcp.begin();
Serial.begin(9600);
pinMode(INTERPIN, INPUT);
mcp.setupInterrupts(true,false,LOW); //enable mirroring of INTA and INTB
mcp.pinMode(TASTER, INPUT);
mcp.pullUp(TASTER, HIGH);
mcp.setupInterruptPin(TASTER,FALLING);
attachInterrupt(digitalPinToInterrupt(INTERPIN), intCallBack, CHANGE);
}
void loop() {
//mcp.digitalRead(TASTER);
}
void intCallBack(){
Serial.println("Interrupt");
}
If I uncomment the line in the main loop the ISR gets executed not without it.
The "Serial.println("Interrupt");" is only for debugging purposes.
I have not worked with that device but you should NEVER perform serial operations in an interrupt. Set a flag and check it in your main loop.
Yes I know.
Therefore I wrote that this is only for debugging purposes.
I would never do it in the final code.
Couple of things:
I think you need a pullup resistor on your digital input from your pushbutton on pin GPA7.
If reading the register allows the interrupt to work, then I suspect that an initial interrupt has already occurred and the code somehow missed it. According to the datasheet, "*The interrupt remains active until the INTCAP *or GPIO register is read". I suspect that reading the GPIO register is clearing the interrupt and therefore the interrupt starts working.
Thank You!
I think you need a pullup resistor on your digital input from your pushbutton on pin GPA7.
I activated the internal pullup with the command
mcp.pullUp(TASTER, HIGH);
didn't I?
If reading the register allows the interrupt to work, then I suspect that an initial interrupt has already occurred and the code somehow missed it. According to the datasheet, "*The interrupt remains active until the INTCAP *or GPIO register is read". I suspect that reading the GPIO register is clearing the interrupt and therefore the interrupt starts working.
This explenation fits exactly to the other observations I made so I think this will be the solution. I'll try it.
Therefore I wrote that this is only for debugging purposes.
I would never do it in the final code.
You should never do that for "debugging purposes" either. Turn on a LED instead.
Okay, thanks. I won't do it in the future. Thank You for the advice!