Attribute VB_Name = "Devantech.Bas" ' Reads direction from Devantech's compass module via I2C interface ' Uses Prof. Anderson's I2C routines (latest with I2C_In_Byte ' as a function) ' www.robot-electronics.co.uk ' www.acroname.com ' www.phanderson.com ' ' BX-24 Devantech ' ' Term 14 ------------------- SCL (term 2) ' Term 13 ------------------- SDA (term 3) ' Note that pullup resistor to +5 VDC are required on both the ' SDA and SCL leads. ' Devantech recommends 1.8K for up to 400KHz, 1.2-1K for up to 1MHz ' Seems to work with 4.7 ' ' Compile with I2C_BX24.Bas ' Public Const SDA_PIN as Byte = 13 ' modify as required Public Const SCL_PIN as Byte = 14 Sub Main() Dim RDIR As Byte Do Call ReadComp(RDIR) 'Returns byte value of 0-255 Debug.Print CStr(RDIR) Call Delay(0.3) Loop End Sub 'Register 1, 0-255 'Register 2&3 0-3599 High Low bytes 'Register 14 0 in calibrate mode, 255 otherwise 'Register 15 Write 255 to enter Cal mode, 0 to exit 'Enter Cal, rotate 360 degrees, look for 255 in reg. 14, Exit Cal. ' Sub ReadComp(ByRef RDIR as Byte) Call I2C_Start() Call I2C_Out_Byte(&HC0) ' Devantech address/select Reg. # Call I2C_Nack() Call I2C_Out_Byte(&H01) ' Reg. # to read Call I2C_Nack() Call I2C_Start() ' ReStart Call I2C_Out_Byte(&HC1) ' Read register Call I2C_Nack() RDIR = I2C_In_Byte() ' Get value Call I2C_Stop() ' No Ack after last byte End Sub