Public Sub MCP3551Read(clckSpeed As Int, chipSelectPin As Pin) As Long
chipSelectPin.DigitalWrite(True)
BeginTransaction(clckSpeed,MSBFIRST,SPI_MODE(3))
chipSelectPin.DigitalWrite(False)
Dim b(4) As Byte
b(2) = Transfer(0x00)
b(1) = Transfer(0x00)
b(0) = Transfer(0x00)
b(3) = 0x00
chipSelectPin.DigitalWrite(True)
Dim res As Long = 0
EndTransaction
Dim r As RandomAccessFile
If UintToBoolean( Bit.Or( Bit.And(b(2),Bit.ShiftLeft(1,6)), Bit.And(b(2),Bit.ShiftLeft(1,7)) )) Then
b(2) = Bit.And(b(2),Bit.Not(Bit.ShiftLeft(1,6)))
Dim r As RandomAccessFile
r.Initialize(b,True)
res = r.ReadLong32(0)
Else if UintToBoolean(Bit.And(b(2),Bit.ShiftLeft(1,5))) Then
r.Initialize(b,True)
res = r.ReadLong32(0)
res = 0x400000-res
End If
Return res
End Sub
Public Sub UintToBoolean(u As UInt) As Boolean
Return u<>0
End Sub