import machine import time i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0)) devices = i2c.scan() addr = devices[0] print('connected to deivce at' + str(hex(addr))) def reg_write(i2c, addr, reg, data): msg = bytearray() msg.append(data) i2c.writeto_mem(addr, reg, msg) def reg_read(i2c, addr, reg, nbytes): data = i2c.readfrom_mem(addr, reg, nbytes) return data # constants for temperature calibration x = reg_read(i2c, addr, 0x88, 2) T1 = x[1]*256 + x[0] if(T1 > 0x8000): T1 = T1 - 0x10000 x = reg_read(i2c, addr, 0x8A, 2) T2 = x[1]*256 + x[0] if(T2 > 0x8000): T2 = T2 - 0x10000 x = reg_read(i2c, addr, 0x8C, 2) T3 = x[1]*256 + x[0] if(T3 > 0x8000): T3 = T3 - 0x10000 # set filter order # filer = 16 # delay = 20ms reg_write(i2c, addr, 0xf5, 0xc0) # set oversampling to 16x reg_write(i2c, addr, 0xf4, 0xff) reg_write(i2c, addr, 0xf2, 0xff) for i in range(0,10): while(ord(reg_read(i2c, addr, 0xf3, 1)) & 0x08): pass x0 = ord(reg_read(i2c, addr, 0xFA, 1)) x1 = ord(reg_read(i2c, addr, 0xFA+1, 1)) x2 = ord(reg_read(i2c, addr, 0xFA+2, 1)) raw = ((x0 << 16) | (x1 << 8) | x2) >> 4 x = raw - (T1<<4) ax2 = (x*x*T3) >> 34 bx = x*T2 >> 14 T = (ax2 + bx) / 5120 print(T1, T2, T3, raw, T) time.sleep(1)