Gusse

- 15th January 2009, 21:27

Hi,

I have been playing with ST's LIS302DL 3-axis accelerometer, which is quite nice IC. It uses I2C bus and device has 2 independed programmable interrupts. I'm using it only for motion detection (alarm) at the moment, but there are huge possibilities with the device :) (mainly due to my limited coding skills, but learning all the time)

Here is piece of my code with comments. I hope somebody find it usefull.

'-------------------------------------------------------------------------------

' 3D Accelerometer

'-------------------------------------------------------------------------------

Accelerometer_Init:

'Command SAD[6:1] SAD[0] = SDO R/W SAD+R/W

I2CDevice_3D_read=$39 'Read 001110 0 1 00111001 (39h)

I2CDevice_3D_write=$38 'Write 001110 0 0 00111000 (38h)

'Axes

I2CAddress_3D = $20 'CTRL_REG1

TEMP = %01000111 '47h -> DR=0, PD=1, FS=0, STP=00, Zen=1, Yen=1, Xen=1 (X,Y,Z enable, Output 100Hz)

gosub I2C_Write_3D '41h = X

'---------------------

'Filters

I2CAddress_3D = $21 'CTRL_REG2

TEMP = %00000111 '00h -> SIM = 0, BOOT = 0, --, FDS = 0, HP FF_WU2 = 0, HP FF_WU1 = 1, HP coeff2 = 1, HP coeff1 = 1 (HP 0.25Hz for FF_WU1)

gosub I2C_Write_3D

'---------------------

'Interrupts

I2CAddress_3D = $22 'CTRL_REG3

TEMP = %00000001 '01h -> IHL = 0, PP_OD = 0, I2CFG2-0 = 000, I1CFG2-0 = 001 (FF_WU 1 = INT1)

gosub I2C_Write_3D

'---------------------

'Sensitivity

I2CAddress_3D = $32 'FF_WU_THS_1

TEMP = $02 ' 0Ah -> DCRM = 0, THS6 = 0, THS5 = 0, THS4 = 0, THS3 = 0, THS2 = 0, THS1 = 1, THS0 = 0 (~36mg wake-up threshold, ~18mg step)

gosub I2C_Write_3D

'---------------------

'Duration

I2CAddress_3D = $33 'FF_DURATION_1

TEMP = $0D '05h -> D7 = 0, D6 = 0, D5 = 0, D4 = 0, D3 = 1, D2 = 1, D1 = 0, D0 = 1 (130ms, 0 - 2.55s, 10ms step)

gosub I2C_Write_3D '0Ah = 100ms, 0Fh = 150ms, 14h = 200ms, 28h = 400ms, 32h = 500ms,

'---------------------

'Wake-Up Thresholds

I2CAddress_3D = $30 'FF_WU_GFG_1

TEMP = %01101010 '6Ah -> AOI = 0, LIR = 1, ZHIE = 1, ZLIE = 0, YHIE = 1, YLIE = 0, XHIE = 1, XLIE = 0 (wake-up above thresholde on x,y,z)

gosub I2C_Write_3D

'---------------------

'I2CAddress_3D = $36 'FF_WU_THS_2

'TEMP = $14 '14h -> DCRM = 0, THS6 = 0, THS5 = 0, THS4 = 1, THS3 = 0, THS2 = 1, THS1 = 0, THS0 = 0 (~350mg wake-up threshold, ~18mg step)

'gosub I2C_Write_3D

''---------------------

'I2CAddress_3D = $37 'FF_DURATION_2

'TEMP = $0A '0Ah -> D7 = 0, D6 = 0, D5 = 0, D4 = 0, D3 = 1, D2 = 0, D1 = 1, D0 = 0 (100ms, 0 - 2.55sec, 10msec step)

'gosub I2C_Write_3D

''---------------------

'I2CAddress_3D = $34 'FF_WU_GFG_2

'TEMP = $78 '78h -> AOI = 0, LIR = 1, ZHIE = 1, ZLIE = 1, YHIE = 1, YLIE = 0, XHIE = 0, XLIE = 0 (wake-up above thresholde on x,y,z)

'gosub I2C_Write_3D

'---------------------

I2CAddress_3D = $23 'Reset acceration/tilt value. Needed if HP enable in CTRL_REG2

gosub I2C_Read_3D

Return

'-------------------------------------------------------------------------------

I2C_Write_3D:

I2CWRITE SDA_3D, SCL_3D, I2CDevice_3D_write, I2CAddress_3D,[ TEMP ]

Return

I2C_Read_3D:

I2cread SDA_3D, SCL_3D, I2CDevice_3D_read, I2CAddress_3D,[ TEMP ]

Return

'-------------------------------------------------------------------------------

Accelerometer:

IF Int1_3D = 1 THEN ' Int1_3D is IO pin for Interrup coming from 3D sensor

I2CAddress_3D = $31 ' Clear interrupt request

gosub I2C_Read_3D

I2CAddress_3D = $23 ' Reset acceration/tilt value after interrupt

gosub I2C_Read_3D

PIR_Alarm = 1 ' This is indecator that something must be done in main program

endif

'IF Int2_3D = 1 THEN ' For 2nd interrupt pin

' I2CAddress_3D = $35

' gosub I2C_Read_3D

' I2CAddress_3D = $23

' gosub I2C_Read_3D

' PIR_Alarm = 1

'endif

return

Accelerometer_Clear: ' If sensor has moved during event hadling, then interrupt must be cleared once before new sequence

I2CAddress_3D = $31

gosub I2C_Read_3D

Return

'-------------------------------------------------------------------------------

More info and datasheets can be found here: http://www.st.com/stonline/products/literature/ds/12726/lis302dl.htm

BR,

-Gusse-

I have been playing with ST's LIS302DL 3-axis accelerometer, which is quite nice IC. It uses I2C bus and device has 2 independed programmable interrupts. I'm using it only for motion detection (alarm) at the moment, but there are huge possibilities with the device :) (mainly due to my limited coding skills, but learning all the time)

Here is piece of my code with comments. I hope somebody find it usefull.

'-------------------------------------------------------------------------------

' 3D Accelerometer

'-------------------------------------------------------------------------------

Accelerometer_Init:

'Command SAD[6:1] SAD[0] = SDO R/W SAD+R/W

I2CDevice_3D_read=$39 'Read 001110 0 1 00111001 (39h)

I2CDevice_3D_write=$38 'Write 001110 0 0 00111000 (38h)

'Axes

I2CAddress_3D = $20 'CTRL_REG1

TEMP = %01000111 '47h -> DR=0, PD=1, FS=0, STP=00, Zen=1, Yen=1, Xen=1 (X,Y,Z enable, Output 100Hz)

gosub I2C_Write_3D '41h = X

'---------------------

'Filters

I2CAddress_3D = $21 'CTRL_REG2

TEMP = %00000111 '00h -> SIM = 0, BOOT = 0, --, FDS = 0, HP FF_WU2 = 0, HP FF_WU1 = 1, HP coeff2 = 1, HP coeff1 = 1 (HP 0.25Hz for FF_WU1)

gosub I2C_Write_3D

'---------------------

'Interrupts

I2CAddress_3D = $22 'CTRL_REG3

TEMP = %00000001 '01h -> IHL = 0, PP_OD = 0, I2CFG2-0 = 000, I1CFG2-0 = 001 (FF_WU 1 = INT1)

gosub I2C_Write_3D

'---------------------

'Sensitivity

I2CAddress_3D = $32 'FF_WU_THS_1

TEMP = $02 ' 0Ah -> DCRM = 0, THS6 = 0, THS5 = 0, THS4 = 0, THS3 = 0, THS2 = 0, THS1 = 1, THS0 = 0 (~36mg wake-up threshold, ~18mg step)

gosub I2C_Write_3D

'---------------------

'Duration

I2CAddress_3D = $33 'FF_DURATION_1

TEMP = $0D '05h -> D7 = 0, D6 = 0, D5 = 0, D4 = 0, D3 = 1, D2 = 1, D1 = 0, D0 = 1 (130ms, 0 - 2.55s, 10ms step)

gosub I2C_Write_3D '0Ah = 100ms, 0Fh = 150ms, 14h = 200ms, 28h = 400ms, 32h = 500ms,

'---------------------

'Wake-Up Thresholds

I2CAddress_3D = $30 'FF_WU_GFG_1

TEMP = %01101010 '6Ah -> AOI = 0, LIR = 1, ZHIE = 1, ZLIE = 0, YHIE = 1, YLIE = 0, XHIE = 1, XLIE = 0 (wake-up above thresholde on x,y,z)

gosub I2C_Write_3D

'---------------------

'I2CAddress_3D = $36 'FF_WU_THS_2

'TEMP = $14 '14h -> DCRM = 0, THS6 = 0, THS5 = 0, THS4 = 1, THS3 = 0, THS2 = 1, THS1 = 0, THS0 = 0 (~350mg wake-up threshold, ~18mg step)

'gosub I2C_Write_3D

''---------------------

'I2CAddress_3D = $37 'FF_DURATION_2

'TEMP = $0A '0Ah -> D7 = 0, D6 = 0, D5 = 0, D4 = 0, D3 = 1, D2 = 0, D1 = 1, D0 = 0 (100ms, 0 - 2.55sec, 10msec step)

'gosub I2C_Write_3D

''---------------------

'I2CAddress_3D = $34 'FF_WU_GFG_2

'TEMP = $78 '78h -> AOI = 0, LIR = 1, ZHIE = 1, ZLIE = 1, YHIE = 1, YLIE = 0, XHIE = 0, XLIE = 0 (wake-up above thresholde on x,y,z)

'gosub I2C_Write_3D

'---------------------

I2CAddress_3D = $23 'Reset acceration/tilt value. Needed if HP enable in CTRL_REG2

gosub I2C_Read_3D

Return

'-------------------------------------------------------------------------------

I2C_Write_3D:

I2CWRITE SDA_3D, SCL_3D, I2CDevice_3D_write, I2CAddress_3D,[ TEMP ]

Return

I2C_Read_3D:

I2cread SDA_3D, SCL_3D, I2CDevice_3D_read, I2CAddress_3D,[ TEMP ]

Return

'-------------------------------------------------------------------------------

Accelerometer:

IF Int1_3D = 1 THEN ' Int1_3D is IO pin for Interrup coming from 3D sensor

I2CAddress_3D = $31 ' Clear interrupt request

gosub I2C_Read_3D

I2CAddress_3D = $23 ' Reset acceration/tilt value after interrupt

gosub I2C_Read_3D

PIR_Alarm = 1 ' This is indecator that something must be done in main program

endif

'IF Int2_3D = 1 THEN ' For 2nd interrupt pin

' I2CAddress_3D = $35

' gosub I2C_Read_3D

' I2CAddress_3D = $23

' gosub I2C_Read_3D

' PIR_Alarm = 1

'endif

return

Accelerometer_Clear: ' If sensor has moved during event hadling, then interrupt must be cleared once before new sequence

I2CAddress_3D = $31

gosub I2C_Read_3D

Return

'-------------------------------------------------------------------------------

More info and datasheets can be found here: http://www.st.com/stonline/products/literature/ds/12726/lis302dl.htm

BR,

-Gusse-