Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。
Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台
Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。
Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。
MPU6050
mian.py
# 在这里写上你的代码 :-) from machine import I2C,Pin import mpu6050,time i2c = I2C(scl=Pin(17), sda=Pin(16)) accelerometer = mpu6050.accel(i2c) while True: print(accelerometer.get_values()) time.sleep_ms(300)
import machine class accel(): def __init__(self, i2c, addr=0x68): self.iic = i2c self.addr = addr self.iic.start() self.iic.writeto(self.addr, bytearray([107, 0])) self.iic.stop() def get_raw_values(self): self.iic.start() a = self.iic.readfrom_mem(self.addr, 0x3B, 14) self.iic.stop() return a def get_ints(self): b = self.get_raw_values() c = [] for i in b: c.append(i) return c def bytes_toint(self, firstbyte, secondbyte): if not firstbyte & 0x80: return firstbyte << 8 | secondbyte return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1) def get_values(self): raw_ints = self.get_raw_values() vals = {} vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1]) vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3]) vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5]) vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53 vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9]) vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11]) vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13]) return vals # returned in range of Int16 # -32768 to 32767 def val_test(self): # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC from time import sleep while 1: print(self.get_values()) sleep(0.05)
Copyright © 2014 ESP56.com All Rights Reserved
执行时间: 0.0077719688415527 seconds