'WIRE LEGEND for PCF8575 I2C 16 bit I/O Extension Shield Module
'PCF8575 to Arduino
'VCC = 5V
'GND = GND
'23 (SDA) = A4
'22 (SCK) = A5
'P0 to P17 = 3.3V to 5V
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private WireMaster As WireMaster
Private TmrReadPCF8575 As Timer
Private I2CAddress As Byte = 0x20
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
WireMaster.Initialize
WireMaster.WriteTo(I2CAddress, Array As Byte(0x00, 0x00)) 'Set all bits to 0. Not really needed in this example but I added it anyway.
'WireMaster.WriteTo(I2CAddress, Array As Byte(0xFF, 0xAA)) 'Set bits to 1111 1111 1010 1010
TmrReadPCF8575.Initialize("TmrReadPCF8575_Tick", 1000)
TmrReadPCF8575.Enabled = True
End Sub
Sub TmrReadPCF8575_Tick
Dim P0ToP7(8), P10ToP17(8) As Byte
BytesToBin(WireMaster.RequestFrom(I2CAddress, 8)(1), P10ToP17) '1 byte equals 8 bits in length
BytesToBin(WireMaster.RequestFrom(I2CAddress, 8)(0), P0ToP7) '1 byte equals 8 bits in length
Log(JoinBytes(Array(P10ToP17, P0ToP7))) 'Join the 2 bytes together to make it 16 bits in length
End Sub
Sub BytesToBin (Bytes As Byte, PCFPins() As Byte)
For i = 0 To PCFPins.Length - 1
If Bit.Get(Bytes, 7 - i) = 1 Then PCFPins(i) = Asc(1) Else PCFPins(i) = Asc(0)
Next
End Sub