# 读MMA7660三轴加速度传感器数据 # 2021/7/31/1/20 import machine import struct import time class MMA7660: sclpin=machine.Pin(11) sdapin=machine.Pin(10) def init(self): global i2c i2c=machine.SoftI2C(scl=MMA7660.sclpin,sda=MMA7660.sdapin,freq=400000) print('MMA7660 address={}'.format(i2c.scan()[0])) # 使能 i2c.writeto_mem(76,7,b'1') time.sleep_ms(10) print('MMA7660 init') def close(self): i2c.writeto_mem(76,7,b'0') print('MMA7660 closed') def _bit2num(self,x): xsign=(x&32)>>5 # 取符号位 x=x&31 # 取数值 if xsign==1: x=-x return x def readxyz(self): x=i2c.readfrom_mem(76,0,8) y=i2c.readfrom_mem(76,1,8) z=i2c.readfrom_mem(76,2,8) # 数据转换 # 6bit结果输出,移位操作 x=struct.unpack('>6 # PoLa 000 unkonwn 001 Left 010 Right 101 Down 110 Up PoLa=(tilt_data&28)>>2 pos=0 if PoLa==0: pos='unknown!' elif PoLa==1: pos='Left' elif PoLa==2: pos='Rignt' elif PoLa==5: pos='Down' elif PoLa==6: pos='Up' return pos