Copyright © Parallax Inc.
Page 5 of 12
GOSUB I2C_Write
GOSUB I2C_Read
rawL = I2C_DATA ' Read high byte
GOSUB I2C_ACK
GOSUB I2C_Read
rawH = I2C_DATA ' Read low byte
GOSUB I2C_ACK
X = (rawH << 8) | rawL ' OR high and low into X
' Do the same for Y and Z:
GOSUB I2C_Read
rawL = I2C_DATA
GOSUB I2C_ACK
GOSUB I2C_Read
rawH = I2C_DATA
GOSUB I2C_ACK
Y = (rawH << 8) | rawL
GOSUB I2C_Read
rawL = I2C_DATA
GOSUB I2C_ACK
GOSUB I2C_Read
rawH = I2C_DATA
GOSUB I2C_NACK
Z = (rawH << 8) | rawL
GOSUB I2C_Stop
RETURN
'---------I2C functions------------
' Read the status register until the ZYXDA bit is high
Wait_For_Data_Ready:
DO
I2C_REG = STATUS_REG
GOSUB I2C_Read_Reg
LOOP UNTIL ((I2C_DATA & $08) <> 0)
RETURN
' Set I2C_REG & I2C_VAL before calling this
I2C_Write_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
I2C_DATA = I2C_VAL
GOSUB I2C_Write
GOSUB I2C_Stop
RETURN
' Set I2C_REG before calling this, I2C_DATA will have result
I2C_Read_Reg: