|
1 |
| -# micropython |
2 |
| - |
3 |
| -# MIT license |
4 |
| - |
5 |
| - |
6 | 1 | # Please read this before use!: https://www.ti.com/product/TMP117
|
7 |
| -from machine import I2C |
8 |
| -import tmp117ti |
| 2 | +from machine import I2C, Pin |
| 3 | +import tmp117timod |
9 | 4 | import time
|
10 |
| -from sensor_pack.bus_service import I2cAdapter |
| 5 | +from sensor_pack_2.bus_service import I2cAdapter |
11 | 6 |
|
12 | 7 | if __name__ == '__main__':
|
13 | 8 | # пожалуйста установите выводы scl и sda в конструкторе для вашей платы, иначе ничего не заработает!
|
14 | 9 | # please set scl and sda pins for your board, otherwise nothing will work!
|
15 | 10 | # https://docs.micropython.org/en/latest/library/machine.I2C.html#machine-i2c
|
16 | 11 | # i2c = I2C(0, scl=Pin(13), sda=Pin(12), freq=400_000) № для примера
|
17 | 12 | # bus = I2C(scl=Pin(4), sda=Pin(5), freq=100000) # на esp8266 !
|
18 |
| - # Внимание!!! |
19 |
| - # Замените id=1 на id=0, если пользуетесь первым портом I2C !!! |
20 |
| - # Warning!!! |
21 |
| - # Replace id=1 with id=0 if you are using the first I2C port !!! |
22 |
| - i2c = I2C(id=1, freq=400_000) # on Arduino Nano RP2040 Connect tested |
| 13 | + # i2c = I2C(id=1, freq=400_000) # on Arduino Nano RP2040 Connect tested |
| 14 | + i2c = I2C(id=1, scl=Pin(7), sda=Pin(6), freq=400_000) # on Raspberry Pi Pico |
23 | 15 | adapter = I2cAdapter(i2c)
|
24 |
| - # ps - pressure sensor |
25 |
| - ts = tmp117ti.TMP117(adapter) |
| 16 | + # ts - temperature sensor |
| 17 | + ts = tmp117timod.TMP117(adapter) |
26 | 18 |
|
27 | 19 | # если у вас посыпались исключения EIO, проверьте все соединения!
|
28 | 20 | # if you're getting EIO exceptions, check all connections!
|
29 | 21 | res = ts.get_id()
|
30 |
| - print(f"chip_id: {hex(res)}") |
| 22 | + print(f"chip_id: {res}") |
31 | 23 | # Таинственное число :-) mysterious number :-)
|
32 |
| - nist = [hex(x) for x in ts.get_nist()] |
33 |
| - print(f"NIST: {nist}") |
| 24 | + print(f"NIST: {ts.get_nist()}") |
34 | 25 | res = ts.get_config()
|
35 | 26 | print(f"config after __init__: {hex(res)}")
|
36 | 27 | ts.conversion_cycle_time = 7
|
|
46 | 37 |
|
47 | 38 | print(20*"*_")
|
48 | 39 | print("Continuous conversion mode!")
|
| 40 | + ts.start_measurement(single_shot=False) |
49 | 41 | for _ in range(5):
|
50 |
| - val = ts.get_temperature() |
| 42 | + val = ts.get_measurement_value() |
51 | 43 | print(f"Temperature: {val} \u2103.\tSleep time: {sleep_time} [ms]")
|
52 | 44 | sleep_time = ts.get_conversion_cycle_time()
|
53 | 45 | time.sleep_ms(sleep_time)
|
54 | 46 |
|
55 | 47 | print(20*"*_")
|
56 | 48 | print("One-shot conversion mode!")
|
57 |
| - ts.conversion_mode = 0x03 |
58 |
| - ts.set_config() # change mode |
59 |
| - for _ in range(100): |
60 |
| - if ts.is_data_ready(): |
61 |
| - val = ts.get_temperature() |
| 49 | + ts.start_measurement(single_shot=True) |
| 50 | + sleep_time = ts.get_conversion_cycle_time() |
| 51 | + for _ in range(10): |
| 52 | + if ts.get_data_status(): |
| 53 | + val = ts.get_measurement_value() |
62 | 54 | print(f"Temperature: {val} \u2103.\tSleep time: {sleep_time} [ms]")
|
63 |
| - ts.conversion_mode = 0x03 |
64 |
| - ts.set_config() # re-launch conversion |
65 |
| - sleep_time = ts.get_conversion_cycle_time() |
66 |
| - # print(f"conversion time: {sleep_time} ms") |
| 55 | + ts.start_measurement(single_shot=True) # re-launch conversion |
67 | 56 | # тройное время сна. 1/3 времени датчик работает и 2/3 времени датчик находится в режиме сна!
|
68 | 57 | time.sleep_ms(3 * sleep_time)
|
69 | 58 |
|
70 | 59 | print(20*"*_")
|
71 | 60 | print("Reading using an iterator!")
|
72 |
| - ts.conversion_mode = 0x00 # Continuous conversion mode |
73 |
| - ts.set_config() # change mode |
| 61 | + # Continuous conversion mode |
| 62 | + ts.start_measurement(single_shot=False) |
| 63 | + sleep_time = ts.get_conversion_cycle_time() |
| 64 | + _lim = 100 |
| 65 | + _min_old = _lim |
| 66 | + _max_old = -1 * _lim |
74 | 67 | for val in ts:
|
75 |
| - sleep_time = ts.get_conversion_cycle_time() |
76 |
| - print(f"Temperature: {val} \u2103.\tSleep time: {sleep_time} [ms]") |
77 | 68 | time.sleep_ms(sleep_time)
|
| 69 | + _min = min(val, _min_old) |
| 70 | + _max = max(val, _max_old) |
| 71 | + print(f"Temperature: {val} \u2103.\tmin: {_min}\tmax: {_max}") |
| 72 | + _min_old = _min |
| 73 | + _max_old = _max |
0 commit comments