I've been working with a simple i2c library (Peter Fleury). I added the following method to simply check if an address is valid on the bus.
uint8_t i2cScan(uint8_t address) { // reset TWI control register TWCR = 0; // transmit START condition TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // wait for end of transmission while( !(TWCR & (1<<TWINT)) ); // load slave address into data register TWDR = address; // start transmission of address TWCR = (1<<TWINT) | (1<<TWEN); // wait for end of transmission while( !(TWCR & (1<<TWINT)) ); // check if the device has acknowledged the READ / WRITE mode uint8_t twst = TW_STATUS & 0xF8; if (twst != TW_MT_SLA_ACK) return 1; return 0; }
I am testing with a MCP23017 i2c expanded. I have configured the address (A0, A1 and A2) to 000 (e.g all tied to ground). This when scanned is address 0x40. Only the 3 LSB are applicable. Using the library I linked below, the address is correct and I can communicate on address 0x40 B0010 0000.
Here is my question. Using the Arduino library, I can scan with this sketch.
I get an address of 0x20. B0100 0000. As you can see the 5 & 6 bit differs. So using 0x40 with the Wire Library does not work but with the Peter Fleury library it does and visa versa. I plan on digging into the Wire library to see why, but I was wondering if someone knows why these 2 libraries interpret the address differently.