Hello,
I'm using a MCP23017 to connect buttons to a teensy 4.1 and I'm getting some problems configuring the interrupts.
here is the code:
The interrupt only works if I call Wire.requestFrom on the loop function, is there any reason for that or Am I missing some I2C configurations of the MCP?
Thanks!
I'm using a MCP23017 to connect buttons to a teensy 4.1 and I'm getting some problems configuring the interrupts.
here is the code:
Code:
#include <Wire.h>
#define PWREN 29
#define TEENSY_INTERRUPT_PIN 4
#define TEENSY_ENCODERS_RESET_PIN 5
#define TEENSY_BUTTONS_RESET_PIN 6
#define I2C_BUTTONS 0x22
//https://github.com/blemasle/arduino-mcp23017/blob/master/src/MCP23017.h
#define I2C_IODIR_A 0x00 // Controls the direction of the data I/O for port A.
#define I2C_IODIR_B 0x01 // Controls the direction of the data I/O for port B.
#define I2C_IPOL_A 0x02 // Configures the polarity on the corresponding GPIO_ port bits for port A.
#define I2C_IPOL_B 0x03 // Configures the polarity on the corresponding GPIO_ port bits for port B.
#define I2C_GPINTEN_A 0x04 // Controls the interrupt-on-change for each pin of port A.
#define I2C_GPINTEN_B 0x05 // Controls the interrupt-on-change for each pin of port B.
#define I2C_DEFVAL_A 0x06 // Controls the default comparaison value for interrupt-on-change for port A.
#define I2C_DEFVAL_B 0x07 // Controls the default comparaison value for interrupt-on-change for port B.
#define I2C_INTCON_A 0x08 // Controls how the associated pin value is compared for the interrupt-on-change for port A.
#define I2C_INTCON_B 0x09 // Controls how the associated pin value is compared for the interrupt-on-change for port B.
#define I2C_IOCON_A 0x0A // Controls the device.
#define I2C_IOCON_B 0x0B // Controls the device.
#define I2C_GPPU_A 0x0C // Controls the pull-up resistors for the port A pins.
#define I2C_GPPU_B 0x0D // Controls the pull-up resistors for the port B pins.
#define I2C_INTF_A 0x0E // Reflects the interrupt condition on the port A pins.
#define I2C_INTF_B 0x0F // Reflects the interrupt condition on the port B pins.
#define I2C_INTCAP_A 0x10 // Captures the port A value at the time the interrupt occured.
#define I2C_INTCAP_B 0x11 // Captures the port B value at the time the interrupt occured.
#define I2C_GPIO_A 0x12 // Reflects the value on the port A.
#define I2C_GPIO_B 0x13 // Reflects the value on the port B.
#define I2C_OLAT_A 0x14 // Provides access to the port A output latches.
#define I2C_OLAT_B 0x15 // Provides access to the port B output latches.
int calls = 0;
void writeI2C(int I2Caddress, byte memAddr, byte memValue) {
Wire.beginTransmission(I2Caddress);
Wire.write(memAddr);
Wire.write(memValue);
Wire.endTransmission();
}
void interruptHandler() {
calls++;
}
void setup() {
Serial.begin(9600);
pinMode(PWREN, OUTPUT);
digitalWrite(PWREN, HIGH);
Wire.begin(); // enable Wire bus
Wire.setClock(400000); //I2C 400kHz
setupI2CButtons();
attachInterrupt(digitalPinToInterrupt(TEENSY_INTERRUPT_PIN), interruptHandler, FALLING);
}
void setupI2CButtons() {
writeI2C(I2C_BUTTONS, I2C_IOCON_A, 0x40); // MIRROR MODE
writeI2C(I2C_BUTTONS, I2C_IOCON_B, 0x40); // MIRROR MODE
// PORT A: encoder switches
writeI2C(I2C_BUTTONS, I2C_IODIR_A, 0xFF); // mode (FF -> input 00; -> output)
writeI2C(I2C_BUTTONS, I2C_GPPU_A, 0x00); // pullup
writeI2C(I2C_BUTTONS, I2C_GPINTEN_A, 0xFF); // interrupt
// PORT B: buttons
writeI2C(I2C_BUTTONS, I2C_IODIR_B, 0xFF); // mode (FF -> input; 00 -> output)
writeI2C(I2C_BUTTONS, I2C_GPPU_B, 0x00); // pullup
writeI2C(I2C_BUTTONS, I2C_GPINTEN_B, 0xFF); // interrupt
}
void loop() {
Wire.requestFrom(I2C_BUTTONS, 2); //request TWO bytes from the pointed location from above
Serial.print("calls:");
Serial.print(calls);
Serial.println(" ");
delay(25);
}
The interrupt only works if I call Wire.requestFrom on the loop function, is there any reason for that or Am I missing some I2C configurations of the MCP?
Thanks!