I have been trying to interface to a digital compass module and have been getting the odd false reading. After a bit of searching through documentation and a guide to I2C (very useful reference: http://www.robot-electronics.co.uk/htm/ ... 2c_bus.htm), I decided that it must be due to the slave stretching the clock (i.e. holding it low whilst it finishes its work before sending a byte). I have altered the ShiftIn function of the SI2C module to include a wait for clock stretching, with a timeout:
Code: Select all
{
****************************************************************************
* Name : ShiftIn (PRIVATE) *
* Purpose : Shift in a byte value, MSB first, sample whilst clock high *
****************************************************************************
}
Function ShiftIn() As Byte
Dim Index As Byte
Dim TimeOut As Byte
Index = 8
Result = 0
Input(SDA)
Repeat
High(SCL)
TimeOut = $FF
Repeat
Dec(TimeOut)
Delay
Until (SCL = 1 Or TimeOut = 0) // wait for any clock stretching by slave
Result = Result << 1
Result.0 = SDA
Low(SCL)
Delay
Dec(Index)
Until Index = 0
Output(SDA)
ClrWDT
End Function
Regards,
Steve