I need to invert the address and the control fields and to multiply the address by 2
Then you are doing something very strange in either your Hardware connections or in Software!
If fact, I believe you've not realised how the chip is to be addressed (refer to Datasheet 1.4.1). If you have GROUNDED pins A0,A1 and A2, then you would address the chip as $40, but if for example A1 and A2 are GROUNDED and you tie A0 to VDD, then you would address the chip as $42. Note that Bit 0 must be left alone, because it determines if this is a READ or WRITE operation!!!! YOU don't touch BIT-0, it is set for you elsewhere by the I2C commands (which also determines that your ADDRESS must be a BYTE VARIABLE and NOT a Constant).
I use MCP23008's in several designs with both 16F's and 18F's. PBP Ver 2.46 thru 2.50 has no problems and no issues and allows me to port software across several PIC families which may not have I2C hardware capability. I commonly mix MCP23008's with EEPROM's and RTC's all on the same bus, and up to five devices on the same bus pins are good with 4K7 Pull-Up's.
I2CWrite SDA,SCL,I2CAddress,I2CRegister,[I2CData]
Where I2CAddress, I2CRegister and I2CData are all BYTES and previously (for example)...
I2CAddress=$42 ' Hardware Address of selected MCP23008 Device
I2CRegister=9 ' Device Register Address being written to
There is no Hardware initialisation required on the PIC side (other than defining your SCL and SDA pins), but yes, you DO have to initialise your MCP23008's before you use them!
Here is a typical initialisation sequence at initial Power-On for TWO MCP's being configured for OUTPUT (with different inital ON states)...
Code:
For CounterA=0 to 1
If CounterA=1 then
I2CAddress=GPA
else
I2CAddress=GPB
endif
For I2CRegister=0 to 9
I2CData=0
If I2CRegister=9 then
If CounterA=0 then
I2CData=%11111111
else
I2CData=%00001100
endif
endif
I2CWrite SDA,SCL,I2CAddress,I2CRegister,[I2CData]
Next I2CRegister
Next CounterA
Refer to the MCP's Datasheet for configuring for your own requirements.
Bookmarks