Attribute VB_Name = "DevantechW.Bas" ' Reads direction from Devantech's compass module via I2C interface ' Uses Prof. Anderson's BX24 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.7K ' ' 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(1 to 2) As Byte ' 2 byte array Dim Degrees As Integer ' 16 bit Do Call ReadComp(RDIR) 'Returns 2 bytes, MSB first ' Shift MSB to hi byte of word(0-14) and add in LSB as low byte(0-255) ' to make 2 byte value 0-3599 corresponding to 0-359.9 degrees Degrees = CInt(RDIR(1))*256 + CInt(RDIR(2)) Debug.Print CStr(Degrees) 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) ' Deavntech address/write reg. # Call I2C_nack() Call I2C_Out_Byte(&H02) ' Reg # to read( 2 bytes) Call I2C_nack() Call I2C_Start() ' ReStart Call I2C_Out_Byte(&HC1) ' Read selected Reg. Call I2C_nack() RDIR(1) = I2C_In_Byte() ' Reg 2, MSB Call I2C_ack() RDIR(2) = I2C_In_Byte() ' Reg 3, LSB Call I2C_Stop() ' No Ack after last byte End Sub