-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLIS33DE.py
More file actions
executable file
·103 lines (85 loc) · 3.94 KB
/
Copy pathLIS33DE.py
File metadata and controls
executable file
·103 lines (85 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# LIS33DE
# This code is designed to work with the LIS33DE_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Accelorometer?sku=LIS33DE_I2CS#tabs-0-product_tabset-2
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# I2C address of the device
LIS33DE_DEFAULT_ADDRESS = 0x1C
# LIS33DE Register Map
LIS33DE_CTRL_REG1 = 0x20 # Accelerometer Control Register 1
LIS33DE_CTRL_REG2 = 0x21 # Accelerometer Control Register 2
LIS33DE_CTRL_REG3 = 0x22 # Accelerometer Control Register 3
LIS33DE_STATUS = 0x27 # Status Register
LIS33DE_OUT_X = 0x29 # X-Axis Acceleration Data Register
LIS33DE_OUT_Y = 0x2B # Y-Axis Acceleration Data Register
LIS33DE_OUT_Z = 0x2D # Z-Axis Acceleration Data Register
LIS33DE_FF_WU_CFG = 0x30 # Free-Fall/Wake-Up Interrupt Configuration Register
LIS33DE_FF_WU_SRC = 0x31 # Free-Fall/Wake-Up Interrupt Source Register
LIS33DE_FF_WU_THS = 0x32 # Free-Fall/Wake-Up Interrupt Threshold Register
LIS33DE_FF_WU_DURATION = 0x33 # Free-Fall/Wake-Up Interrupt Duration Register
# LIS33DE Control Register-1 Configuration
LIS33DE_CTRL_REG1_AODR_100 = 0x00 # ODR: 100 Hz
LIS33DE_CTRL_REG1_AODR_400 = 0x80 # ODR: 400 Hz
LIS33DE_CTRL_REG1_PD_PWRDWN = 0x00 # Power-Down Mode
LIS33DE_CTRL_REG1_PD_ACTIVE = 0x40 # Active Mode
LIS33DE_CTRL_REG1_FS_2G = 0x00 # +/- 2G
LIS33DE_CTRL_REG1_FS_8G = 0x20 # +/- 8G
LIS33DE_CTRL_REG1_ST_NORMAL = 0x00 # Self Test Normal
LIS33DE_CTRL_REG1_STP_ENABLED = 0x10 # Self-Test P Enabled
LIS33DE_CTRL_REG1_STM_ENABLED = 0x08 # Self-Test M Enabled
LIS33DE_CTRL_REG1_AZEN_ENABLE = 0x04 # Acceleration Z-Axis Enabled
LIS33DE_CTRL_REG1_AYEN_ENABLE = 0x02 # Acceleration Y-Axis Enabled
LIS33DE_CTRL_REG1_AXEN_ENABLE = 0x01 # Acceleration X-Axis Enabled
# LIS33DE Control Register-2 Configuration
LIS33DE_CTRL_REG2_SIM_4WIRE = 0x00 # 4-Wire Interface
LIS33DE_CTRL_REG2_SIM_3WIRE = 0x80 # 3-Wire Interface
LIS33DE_CTRL_REG2_BOOT_NORMAL = 0x00 # Normal Mode
LIS33DE_CTRL_REG2_BOOT_REBOOT = 0x40 # Reboot Memory Content
class LIS33DE():
def __init__(self):
self.datarate_config()
self.mode_config()
def datarate_config(self):
""" Select the Control Register-1 Configuration from the given provided value above"""
DATARATE_CONFIG = (LIS33DE_CTRL_REG1_PD_ACTIVE | LIS33DE_CTRL_REG1_AXEN_ENABLE | LIS33DE_CTRL_REG1_AYEN_ENABLE | LIS33DE_CTRL_REG1_AZEN_ENABLE)
bus.write_byte_data(LIS33DE_DEFAULT_ADDRESS, LIS33DE_CTRL_REG1, DATARATE_CONFIG)
def mode_config(self):
""" Select the Control Register-2 Configuration from the given provided value above"""
MODE_CONFIG = (LIS33DE_CTRL_REG2_SIM_4WIRE | LIS33DE_CTRL_REG2_BOOT_NORMAL)
bus.write_byte_data(LIS33DE_DEFAULT_ADDRESS, LIS33DE_CTRL_REG2, MODE_CONFIG)
def read_accl(self):
"""Read data back from LIS33DE_OUT_X(0x29), 1 byte
X-Axis Accl"""
data = bus.read_byte_data(LIS33DE_DEFAULT_ADDRESS, LIS33DE_OUT_X)
# Convert the data
xAccl = data
if xAccl > 127 :
xAccl -= 256
"""Read data back from LIS33DE_OUT_Y(0x2B), 1 byte
Y-Axis Accl"""
data = bus.read_byte_data(LIS33DE_DEFAULT_ADDRESS, LIS33DE_OUT_Y)
# Convert the data
yAccl = data
if yAccl > 127 :
yAccl -= 256
"""Read data back from LIS33DE_OUT_Z(0x2D), 1 byte
Z-Axis Accl"""
data = bus.read_byte_data(LIS33DE_DEFAULT_ADDRESS, LIS33DE_OUT_Z)
# Convert the data
zAccl = data
if zAccl > 127 :
zAccl -= 256
return {'x' : xAccl, 'y' : yAccl, 'z' : zAccl}
from LIS33DE import LIS33DE
lis33de = LIS33DE()
while True:
accl = lis33de.read_accl()
print "Acceleration in X-Axis : %d"%(accl['x'])
print "Acceleration in Y-Axis : %d"%(accl['y'])
print "Acceleration in Z-Axis : %d"%(accl['z'])
print " ************************************* "
time.sleep(1)