Skip to content

Commit fbc2f38

Browse files
committed
add sc7a20 driver.
1 parent 06d703f commit fbc2f38

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module("sc7a20.py", opt=3)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# SC7A20 driver for MicroPython on ESP8266/ESP32
2+
# MIT license; Copyright (c) 2022 WEMOS.CC
3+
4+
from micropython import const
5+
6+
7+
WHO_AM_I_REG = const(0x0F)
8+
CTRL_REG1 = const(0x20)
9+
CTRL_REG2 = const(0x21)
10+
CTRL_REG3 = const(0x22)
11+
ADDR_STATUS_REG = const(0x27)
12+
OUT_X_L_REG = const(0x28)
13+
OUT_X_H_REG = const(0x29)
14+
OUT_Y_L_REG = const(0x2A)
15+
OUT_Y_H_REG = const(0x2B)
16+
OUT_Z_L_REG = const(0x2C)
17+
OUT_Z_H_REG = const(0x2D)
18+
19+
CHIP_ID = const(0x11)
20+
21+
22+
class SC7A20:
23+
def __init__(self, i2c, address=0x18):
24+
self.bus = i2c
25+
self.slv_addr = address
26+
self.buf = bytearray(6)
27+
self.bus.writeto_mem(self.slv_addr, CTRL_REG1, b"\x27") # 10hz
28+
29+
def _12bitComplement(self, msb, lsb):
30+
# temp=0x0000
31+
temp = msb << 8 | lsb
32+
temp = temp >> 4
33+
# 只有高12位有效
34+
temp = temp & 0x0FFF
35+
36+
if temp & 0x0800: # 负数 补码==>原码
37+
temp = temp & 0x07FF # 屏弊最高位
38+
temp = ~temp
39+
temp = temp + 1
40+
temp = temp & 0x07FF
41+
temp = -temp # 还原最高位
42+
43+
return temp
44+
45+
def measure(self):
46+
47+
self.buf = self.bus.readfrom_mem(self.slv_addr, OUT_X_L_REG, 6)
48+
accel_X = self._12bitComplement(self.buf[1], self.buf[0])
49+
accel_Y = self._12bitComplement(self.buf[3], self.buf[2])
50+
accel_Z = self._12bitComplement(self.buf[5], self.buf[4])
51+
return accel_X, accel_Y, accel_Z

0 commit comments

Comments
 (0)