'*************************************************
'**LIS302DL Example Program uisng I2C for PICAXE**
'**By Hussam EL-Sheikh 6th July 2009**********
'*************************************************
symbol who = b0 'who am I
symbol dat = b1 'data ready
symbol X= w1
symbol Y= w2
symbol Z= w3
symbol TEMP = w4 'for use in calculations
symbol sign = b10 'sign byte
symbol ii = b11 'counter
low 6
let ii = 0
let X = 0
let Y = 0
let Z = 0
let sign = %00000000 'This is the sign byte, this is how it works: Bit0 is for the X axis
'Bit1 is for the Y axis
'Bit2 is for the Z axis
'When the respective Bit is = 1 then the reading is negative
init:
high 6
hi2csetup i2cmaster, $39, i2cslow, i2cbyte 'set up the I2C protocol for the LIS302DL
hi2cout $20, (%01000111) 'Diable power down and enable X,Y,Z axis
pause 10
data_check:
hi2cin $27,(dat) 'Check if new data is ready
dat = dat | %11110111 'isolate the ZYXDA bit with bit operation
pause 10
if dat = 255 then goto read_data
goto data_check
read_data:
hi2cin $0F,(who) 'Read who_am_i register sanity check should equal %00111011 or $3B
'*************************
hi2cin $29,(TEMP) 'Read X
if TEMP > 127 then 'if it's negative remove the sign and raise a marker
TEMP = 256 - TEMP
TEMP = TEMP*18
X = X + TEMP
sign = sign | %00000001 'Bit operation to signal it's negative
else
TEMP = TEMP*18
X = X + TEMP
sign = sign &/ %00000001 'Bit operation to clear negative signal
endif
'**************************
hi2cin $2B,(TEMP) 'Read Y
if TEMP > 127 then 'if it's negative remove the sign and raise a marker
TEMP = 256 - TEMP
TEMP = TEMP*18
Y = Y + TEMP
sign = sign | %00000010 'Bit operation to signal it's negative
else
TEMP = TEMP*18
Y = Y + TEMP
sign = sign &/ %00000010 'Bit operation to clear negative signal
endif
'*************************
hi2cin $2D,(TEMP) 'Read Z
if TEMP > 127 then 'if it's negative remove the sign and raise a marker
TEMP = 256 - TEMP
TEMP = TEMP*18
Z = Z + TEMP
sign = sign | %00000100 'Bit operation to signal it's negative
else
TEMP = TEMP*18
Z = Z + TEMP
sign = sign &/ %00000100 'Bit operation to clear negative signal
endif
ii = ii+1
'***************************
if ii = 10 then 'Take an average over 10 cycles
ii = 0
X = X/10
Y = Y/10
Z = Z/10
debug
X = 0
Y = 0
Z = 0
endif
goto data_check