11"""
22Driver for accelerometer on STM32F4 Discover board.
33
4- Assumes it's a LIS302DL MEMS device.
54Sets accelerometer range at +-2g.
65Returns list containing X,Y,Z axis acceleration values in 'g' units (9.8m/s^2).
76
2019
2120READWRITE_CMD = const (0x80 )
2221MULTIPLEBYTE_CMD = const (0x40 )
23- LIS302DL_WHO_AM_I_ADDR = const (0x0f )
22+ WHO_AM_I_ADDR = const (0x0f )
23+ OUT_X_ADDR = const (0x29 )
24+ OUT_Y_ADDR = const (0x2b )
25+ OUT_Z_ADDR = const (0x2d )
26+ OUT_T_ADDR = const (0x0c )
27+
2428LIS302DL_WHO_AM_I_VAL = const (0x3b )
2529LIS302DL_CTRL_REG1_ADDR = const (0x20 )
26- LIS302DL_OUT_X = const (0x29 )
27- LIS302DL_OUT_Y = const (0x2b )
28- LIS302DL_OUT_Z = const (0x2d )
2930# Configuration for 100Hz sampling rate, +-2g range
3031LIS302DL_CONF = const (0b01000111 )
3132
32- def convert_raw_to_g (x ):
33- if x & 0x80 :
34- x = x - 256
35- return x * 18 / 1000
33+ LIS3DSH_WHO_AM_I_VAL = const (0x3f )
34+ LIS3DSH_CTRL_REG4_ADDR = const (0x20 )
35+ LIS3DSH_CTRL_REG5_ADDR = const (0x24 )
36+ # Configuration for 100Hz sampling rate, +-2g range
37+ LIS3DSH_CTRL_REG4_CONF = const (0b01100111 )
38+ LIS3DSH_CTRL_REG5_CONF = const (0b00000000 )
3639
3740class STAccel :
3841 def __init__ (self ):
3942 self .cs_pin = Pin ('PE3' , Pin .OUT_PP , Pin .PULL_NONE )
4043 self .cs_pin .high ()
4144 self .spi = SPI (1 , SPI .MASTER , baudrate = 328125 , polarity = 0 , phase = 1 , bits = 8 )
42- self .write_bytes (LIS302DL_CTRL_REG1_ADDR , bytearray ([LIS302DL_CONF ]))
43- if self .read_id () != LIS302DL_WHO_AM_I_VAL :
44- raise Exception ('LIS302DL accelerometer not present' )
45+
46+ self .who_am_i = self .read_id ()
47+
48+ if self .who_am_i == LIS302DL_WHO_AM_I_VAL :
49+ self .write_bytes (LIS302DL_CTRL_REG1_ADDR , bytearray ([LIS302DL_CONF ]))
50+ self .sensitivity = 18
51+ elif self .who_am_i == LIS3DSH_WHO_AM_I_VAL :
52+ self .write_bytes (LIS3DSH_CTRL_REG4_ADDR , bytearray ([LIS3DSH_CTRL_REG4_CONF ]))
53+ self .write_bytes (LIS3DSH_CTRL_REG5_ADDR , bytearray ([LIS3DSH_CTRL_REG5_CONF ]))
54+ self .sensitivity = 0.06 * 256
55+ else :
56+ raise Exception ('LIS302DL or LIS3DSH accelerometer not present' )
57+
58+ def convert_raw_to_g (self , x ):
59+ if x & 0x80 :
60+ x = x - 256
61+ return x * self .sensitivity / 1000
4562
4663 def read_bytes (self , addr , nbytes ):
4764 if nbytes > 1 :
@@ -65,19 +82,16 @@ def write_bytes(self, addr, buf):
6582 self .cs_pin .high ()
6683
6784 def read_id (self ):
68- return self .read_bytes (LIS302DL_WHO_AM_I_ADDR , 1 )[0 ]
85+ return self .read_bytes (WHO_AM_I_ADDR , 1 )[0 ]
6986
7087 def x (self ):
71- return convert_raw_to_g (self .read_bytes (LIS302DL_OUT_X , 1 )[0 ])
88+ return self . convert_raw_to_g (self .read_bytes (OUT_X_ADDR , 1 )[0 ])
7289
7390 def y (self ):
74- return convert_raw_to_g (self .read_bytes (LIS302DL_OUT_Y , 1 )[0 ])
91+ return self . convert_raw_to_g (self .read_bytes (OUT_Y_ADDR , 1 )[0 ])
7592
7693 def z (self ):
77- return convert_raw_to_g (self .read_bytes (LIS302DL_OUT_Z , 1 )[0 ])
94+ return self . convert_raw_to_g (self .read_bytes (OUT_Z_ADDR , 1 )[0 ])
7895
7996 def xyz (self ):
80- val = self .read_bytes (LIS302DL_OUT_X , 5 )
81- return [convert_raw_to_g (val [0 ]),
82- convert_raw_to_g (val [2 ]),
83- convert_raw_to_g (val [4 ])]
97+ return (self .x (), self .y (), self .z ())
0 commit comments