Problem with Interrupt on PCF8574 I2C i/o expander

Hi there,

I’m still new to the Arduino world so I’m hoping im making a silly mistake and my problem can be easily resolved.

Basically i’m in the process of writing up test code for different parts of my project. At the moment i’m trying to get the interrupt working on the PCF8574 i/o expander. I can get the program to recognise a change of state and it will go into the ISR but if i try and read or write to the PCF8574 during this routine the program will hang.

here is the code i’m working with atm

#include <Wire.h>

#define expander B0100000
#define expander2 B0100111

void setup()
{
Serial.begin(9600);
Wire.begin();
attachInterrupt(0, flash, CHANGE);
}

void loop()
{
Serial.println(expanderRead(), BIN);
delay(2000);

}

void flash()
{
Serial.println(“state change”);
Serial.println(expanderRead(), BIN);
}

void expanderWrite(byte _data )
{
Wire.beginTransmission(expander2);
Wire.send(_data);
Wire.endTransmission();
}

byte expanderRead()
{
byte _data;
Wire.requestFrom(expander2, 1);
if(Wire.available()) {
_data = Wire.receive();
}
return _data;
}

the output was comes out like this

"10001001
10001001

state change"

it won’t go any further and i don’t understand why. Any help would be greatly appreciated.

Cheers

I don't think you can use the I2C from within an interrupt service routine as it uses a timer that is interrupt driven. Therefore you might need to re enable the interrupts inside the ISR. However, I don't know if the compiler will cope with that.

The compiler will be just fine if you wanted to sei() within an interrupt service routine. It will even run okay, if you can otherwise guarantee that the same ISR won't be called again before it is able to return (say, the possible interrupt triggers are 1Hz but the routine takes 30usec to handle). But it's way-not-recommended.