Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from machine import I2C
- import utime
- class adt7410_class:
- def __init__(self, i2c, i2c_address):
- self.i2c = i2c
- self.i2c_address = i2c_address
- self.reset()
- def reset(self):
- # リセット
- self.i2c.writeto(self.i2c_address, bytearray([0x2f]))
- # リセット後、動作するまでに200ms必要
- wait01 = utime.ticks_us()
- while True:
- utime.sleep(200/1000000)
- if (utime.ticks_us() - wait01) >= 200:
- break;
- def readdata(self):
- # 16bit 1-shot
- data = [0xa0]
- # 13bit 1-shot
- #data = [0x20]
- self.i2c.writeto_mem(self.i2c_address, 0x03, bytearray(data))
- read_wait_time = utime.ticks_ms()
- read_wait = 22
- utime.sleep(0.01*read_wait)
- # 気温データを読み取りできる状態になるまで待機
- # (ステータスレジスタのbit7が0になると準備OK)
- count001 = 0
- while True:
- wait01 = utime.ticks_ms()
- while True:
- utime.sleep(0.01)
- if (utime.ticks_ms() - wait01) >= 10:
- break;
- # ステータスレジスタの読み込み
- status_reg = bytearray(self.i2c.readfrom_mem(self.i2c_address, 0x02, 1))
- if count001 >= 500:
- break
- if ((int(status_reg[0]) >> 7) & 1) == 0:
- break
- count001 += 1
- status_str = "status=0x{0:02x}, count001={1:d}, {2:d} ms".format(status_reg[0], count001, (utime.ticks_ms()-read_wait_time))
- #print(status_str)
- # 気温データの読み取り
- temp_raw = bytearray(self.i2c.readfrom_mem(self.i2c_address, 0, 2))
- #print("temp_raw = 0x{0:02x}, 0x{1:02x}".format(temp_raw[0], temp_raw[1]))
- # 16bitの計算
- temp = ((temp_raw[0]) << 8) | (temp_raw[1])
- if temp >= 32768:
- temp = temp - 65536
- temp = temp / 128.0
- # 13bitの計算
- #temp = (((temp_raw[0]) << 8) | (temp_raw[1])) >> 3
- #if temp >= 4096:
- # temp - 8192
- #temp = temp / 16
- return temp
- if __name__ == '__main__':
- i2c = I2C(I2C.I2C0, freq=400000, scl=30, sda=31)
- adt7410_01 = adt7410_class(i2c, 0x48)
- while True:
- temp = adt7410_01.readdata()
- print ("{0:.4f} ℃".format(temp))
- time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement