22Driver for accelerometer on STM32F4 Discover board.
33
44Assumes it's a LIS302DL MEMS device.
5-
6- Not currently working .
5+ Sets accelerometer range at +-2g.
6+ Returns list containing X,Y,Z axis acceleration values in 'g' units (9.8m/s^2) .
77
88See:
99 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.h
2222MULTIPLEBYTE_CMD = const (0x40 )
2323LIS302DL_WHO_AM_I_ADDR = const (0x0f )
2424LIS302DL_CTRL_REG1_ADDR = const (0x20 )
25+ LIS302DL_OUT_X = const (0x29 )
26+ # Configuration for 100Hz sampling rate, +-2g range
27+ LIS302DL_CONF = 0b01000111
28+
29+ def signed8 (x ):
30+ if x & 0x80 :
31+ return x - 256
32+ else :
33+ return x
2534
2635class STAccel :
2736 def __init__ (self ):
2837 self .cs_pin = Pin ('PE3' , Pin .OUT_PP , Pin .PULL_NONE )
2938 self .cs_pin .high ()
3039 self .spi = SPI (1 , SPI .MASTER , baudrate = 328125 , polarity = 0 , phase = 1 , bits = 8 )
40+ self .wr (LIS302DL_CTRL_REG1_ADDR , bytearray ([LIS302DL_CONF ]))
3141
3242 def rd (self , addr , nbytes ):
3343 if nbytes > 1 :
@@ -52,5 +62,9 @@ def wr(self, addr, buf):
5262 def read_id (self ):
5363 return self .rd (LIS302DL_WHO_AM_I_ADDR , 1 )
5464
55- def init (self , init_param ):
56- self .wr (LIS302DL_CTRL_REG1_ADDR , bytearray ([init_param ]))
65+ def get_xyz (self ):
66+ val = self .rd (LIS302DL_OUT_X , 5 )
67+ x = signed8 (val [0 ]) * 18.0 / 1000
68+ y = signed8 (val [2 ]) * 18.0 / 1000
69+ z = signed8 (val [4 ]) * 18.0 / 1000
70+ return [x , y , z ]
0 commit comments