|
1 |
| -import time |
2 |
| -import board |
3 |
| -from adafruit_ina260 import INA260, Mode, ConversionTime |
4 |
| - |
5 |
| - |
6 |
| -if __name__ == "__main__": |
7 |
| - try: |
8 |
| - i2c = board.I2C() |
9 |
| - ina260 = INA260(i2c) |
10 |
| - except RuntimeError as r_e: |
11 |
| - # catch exception on init, no INA260 chip found |
12 |
| - print(r_e) |
13 |
| - raise r_e |
14 |
| - else: |
15 |
| - # set overcurrent limit flag and threshold value |
16 |
| - # 0x0008 x 1,25 mA = 10 mA as alert limit |
17 |
| - ina260.alert_limit = 0x0008 |
18 |
| - |
19 |
| - # alert pin is asserted, can be check with gpiozero |
20 |
| - ina260.overcurrent_limit = True |
21 |
| - |
22 |
| - # keep the flag high until MASK_ENABLE register will be read |
23 |
| - ina260.alert_latch_enable = True |
24 |
| - |
25 |
| - ina260.mode = Mode.CONTINUOUS |
26 |
| - |
27 |
| - # set higher conversion time and wait its value before each read |
28 |
| - ina260.current_conversion_time = ConversionTime.TIME_8_244_ms |
29 |
| - for _ in enumerate(range(5)): |
30 |
| - time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) |
31 |
| - print( |
32 |
| - "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" |
33 |
| - % (ina260.current, ina260.voltage, ina260.power) |
34 |
| - ) |
35 |
| - |
36 |
| - # supposing meanwhile the alert limit was exceeded, setting an higher limit |
37 |
| - # and clear the ALERT |
38 |
| - # 0x0100 x 1,25 mA = 320 mA as alert limit |
39 |
| - ina260.alert_limit = 0x0100 |
40 |
| - |
41 |
| - # alert function flag bit should be true if alert threshold was exceeded |
42 |
| - print("Alert function flag: {}".format(ina260.alert_function_flag)) |
43 |
| - |
44 |
| - # in latch mode, reading the register clears the ALERT & alert function flag |
45 |
| - print("MASK register: {}".format(ina260.mask_enable)) |
46 |
| - |
47 |
| - # reset the whole chip and wait 2 sec |
48 |
| - ina260.reset_bit = True |
49 |
| - time.sleep(2) |
50 |
| - print( |
51 |
| - "MASK_REGISTER check, must be 0x0000 after reset: {}".format( |
52 |
| - ina260.mask_enable |
53 |
| - ) |
54 |
| - ) |
| 1 | +import time |
| 2 | +import board |
| 3 | +from adafruit_ina260 import INA260, Mode, ConversionTime |
| 4 | + |
| 5 | + |
| 6 | +if __name__ == "__main__": |
| 7 | + try: |
| 8 | + i2c = board.I2C() |
| 9 | + ina260 = INA260(i2c) |
| 10 | + except RuntimeError as r_e: |
| 11 | + # catch exception on init, no INA260 chip found |
| 12 | + print(r_e) |
| 13 | + raise r_e |
| 14 | + else: |
| 15 | + # set overcurrent limit flag and threshold value |
| 16 | + # 0x0008 x 1,25 mA = 10 mA as alert limit |
| 17 | + ina260.alert_limit = 0x0008 |
| 18 | + |
| 19 | + # alert pin is asserted, can be check with gpiozero |
| 20 | + ina260.overcurrent_limit = True |
| 21 | + |
| 22 | + # keep the flag high until MASK_ENABLE register will be read |
| 23 | + ina260.alert_latch_enable = True |
| 24 | + |
| 25 | + ina260.mode = Mode.CONTINUOUS |
| 26 | + |
| 27 | + # set higher conversion time and wait its value before each read |
| 28 | + ina260.current_conversion_time = ConversionTime.TIME_8_244_ms |
| 29 | + for _ in enumerate(range(5)): |
| 30 | + time.sleep(ConversionTime.get_seconds(ina260.current_conversion_time)) |
| 31 | + print( |
| 32 | + "Current: %.2f mA Voltage: %.2f V Power:%.2f mW" |
| 33 | + % (ina260.current, ina260.voltage, ina260.power) |
| 34 | + ) |
| 35 | + |
| 36 | + # supposing meanwhile the alert limit was exceeded, setting an higher limit |
| 37 | + # and clear the ALERT |
| 38 | + # 0x0100 x 1,25 mA = 320 mA as alert limit |
| 39 | + ina260.alert_limit = 0x0100 |
| 40 | + |
| 41 | + # alert function flag bit should be true if alert threshold was exceeded |
| 42 | + print("Alert function flag: {}".format(ina260.alert_function_flag)) |
| 43 | + |
| 44 | + # in latch mode, reading the register clears the ALERT & alert function flag |
| 45 | + print("MASK register: {}".format(ina260.mask_enable)) |
| 46 | + |
| 47 | + # reset the whole chip and wait 2 sec |
| 48 | + ina260.reset_bit = True |
| 49 | + time.sleep(2) |
| 50 | + print( |
| 51 | + "MASK_REGISTER check, must be 0x0000 after reset: {}".format( |
| 52 | + ina260.mask_enable |
| 53 | + ) |
| 54 | + ) |
0 commit comments