I'm trying to read from an MCP23008 I/O expander using the Wire library over I2C. The MCP23008 is wired to address 0x04, giving an effective device address of 0x24. Writing to the device GPIO seems to work fine, and I'm a bit confused on how to read from the GPIO.
So I set it up for read as follows:
#define MCP23008_DEVICE_I2C_ADDRESS 0x20
#define MCP23008_SLAVE_I2C_ADDRESS 0x04
#define MCP23008_REGISTER_IODIR 0x00
#define MCP23008_REGISTER_GPIO 0x09
#define IO_ALL_PINS_INPUT 0xFF
inline void eeprog::_read_enable()
{
Wire.beginTransmission(MCP23008_DEVICE_I2C_ADDRESS | MCP23008_SLAVE_I2C_ADDRESS);
{
Wire.write(MCP23008_REGISTER_IODIR); // Set I/O direction
Wire.write(IO_ALL_PINS_INPUT); // Set all pins as input
}
Wire.endTransmission();
}
after which I attempt to read from it, by first addressing the GPIO register and then requesting a byte to read:
inline uint8_t eeprog::_read()
{
Wire.beginTransmission(MCP23008_DEVICE_I2C_ADDRESS | MCP23008_SLAVE_I2C_ADDRESS);
{
Wire.write(MCP23008_REGISTER_GPIO); // Select the GPIO register
}
Wire.endTransmission();
Wire.requestFrom(MCP23008_DEVICE_I2C_ADDRESS | MCP23008_SLAVE_I2C_ADDRESS, (uint8_t)1);
return Wire.read();
}
Looking at my logic analyzer this is what I get:
The NACK seems to indicate that the read went wrong.. actually a bit unsure what "Data read: 07" means in this context, did it read the value 7, or did it try read address 07?
Any ideas what I could be doing wrong? Am I missing something obvious from the documentation?
