Jun 272009
 
I2C Port Expander

Finally I had a bit time to play with my Propeller board I got sitting around for half  a year now. I wish it had more I/O ports, but with an “Remote 8-bit I/O expander for I2C-bus” this is a solvable problem. Only got to write a driver for it.

Which turns out to be far easier than I thought.

Here is the driver program. All of it. It’s using an I2C driver but that one is surprisingly simple too.

OBJ
i2cObject         : “Basic_I2C_Driver”

PUB WritePort(i2cSCL,_deviceAddress,i2cData)
i2cObject.Start (i2cSCL)
i2cObject.Write (i2cSCL,_deviceAddress | 0)
i2cObject.Write (i2cSCL, i2cData)
i2cObject.Stop(i2cSCL)

PUB ReadPort(i2cSCL,_deviceAddress) : i2cData
i2cObject.Start (i2cSCL)
i2cObject.Write (i2cSCL,_deviceAddress | 1)
i2cData := i2cObject.Read(i2cSCL,i2cObject#NAK)
i2cObject.Stop(i2cSCL)
return i2cData

And now to use it:

‘ setup i2cobject
i2cObject.Initialize(i2cSCL)

PCF8574AObject.WritePort(i2cSCL,PCF8574A_Addr,%1111_0000)

That will lit LED 0-3 and turn LED 4-7 off. Simple eh? And worked on the first try!