Soft I2C and Programmable I2C address
While writing the mcp4728 library, one of most difficult thing was implementing programmable I2C address.
Single I2C bus can be used to connect multiple I2C devices, so addressing a specific device on the bus require a specific address for the device. Normal I2C devices have couple of pins for selectable address. Address of device is determined by whether address pins (ADx) is ground or VCC. There are some devices like mcp4728 has a advanced way of selectable address. Without physical pins, their address can be programmed and stored in EEPROM of chip. I love this feature of mcp4728, since it allow to stack the shield without physical jumpers.
Microchip’s implementation of programmable address is slightly non-standard, because it require another pin other than two I2C pins. While I understand why they did this way, it make a bit difficult to implement in arduino library. It is not because of requirement of third pin, but because of critical timing of pin latch. Let’s take a look at datasheet how programmable address works.
As you see, LDAC pin need to be latched between 8th and 9th clock. The problem was AVR’s hardware I2C don’t allow me to do it. AVR’s TWI register return it’s status after it receive the ACK from slave device. There was no way me to know when to latch, since TWI register only return status after 9th clock (ACK). So, I need to look for Software I2C library (bit-bang).
There are couple of software I2C library for arduino. I found that SoftI2C from fat16lib is most useful for this.
Also, simple logic logger for arduino was quite handy for debugging.
Dotted blue line show what I can do with wire library of arduino. Notice LDAC latch after ACK ( 9th clcok).
By modifying softI2C library, I was able to latch LDAC pin between 8th and 9th clock.
It is included in mcp4728 library and you can find modified SoftI2C library from google code.
Arduino sketch for reading and writing address of mcp4728.