Skip to content

Commit

Permalink
fix LSB constants + add is_ready property
Browse files Browse the repository at this point in the history
  • Loading branch information
barbudor committed Jan 4, 2021
1 parent 39fe4a4 commit cf73188
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
11 changes: 9 additions & 2 deletions barbudor_ina3221/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@
C_DIE_ID = const(0x3220)

# General constants
C_BUS_ADC_LSB = const(0.008) # VBus ADC LSB is 8mV
C_SHUNT_ADC_LSB = const(0.00004) # VShunt ADC LSB is 40µV
C_BUS_ADC_LSB = 0.008 # VBus ADC LSB is 8mV
C_SHUNT_ADC_LSB = 0.00004 # VShunt ADC LSB is 40µV


class INA3221:
"""Driver class for Texas Instruments INA3221 3 channel current sensor device"""
Expand Down Expand Up @@ -256,3 +257,9 @@ def set_shunt_warning_alert_limit(self, channel, voltage):
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_unsigned(round(voltage * C_SHUNT_ADC_LSB) * 8)
self.write(C_REG_WARNING_ALERT_LIMIT_CH[channel], value)

@property
def is_ready(self):
"""Returns the CVRF (ConVersion Ready Flag) from the mask/enable register """
regvalue = self.read(C_REG_MASK_ENABLE)
return (regvalue & C_CONV_READY_FLAG) != 0
6 changes: 3 additions & 3 deletions barbudor_ina3221/lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@
_DIE_ID = const(0x3220)

# General constants
C_BUS_ADC_LSB = const(0.008) # VBus ADC LSB is 8mV
C_SHUNT_ADC_LSB = const(0.00004) # VShunt ADC LSB is 40µV
C_BUS_ADC_LSB = 0.008 # VBus ADC LSB is 8mV
C_SHUNT_ADC_LSB = 0.00004 # VShunt ADC LSB is 40µV


class INA3221:
Expand Down Expand Up @@ -230,4 +230,4 @@ def bus_voltage(self, channel=1):
#assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_signed(self.read(_REG_BUS_VOLTAGE_CH[channel])) / 8
# convert to volts - LSB = 8mV
return value * C_BUS_ADC_LSB
return value * C_BUS_ADC_LSB
9 changes: 8 additions & 1 deletion examples/ina3221_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
if INA3221.IS_FULL_API:
print("full API sample: improve accuracy")
# improve accuracy by slower conversion and higher averaging
# each conversion now takes 128*0.008 = 1.024 sec
# which means 2 seconds per channel
ina3221.update(reg=C_REG_CONFIG,
mask=C_AVERAGING_MASK |
C_VBUS_CONV_TIME_MASK |
Expand All @@ -37,6 +39,11 @@
# pylint: disable=bad-whitespace

while True:
if INA3221.IS_FULL_API: # is_ready available only in "full" variant
while not ina3221.is_ready:
print(".",end='')
time.sleep(0.1)
print("")

print("------------------------------")
line_title = "Measurement "
Expand Down Expand Up @@ -64,4 +71,4 @@
print(line_shunt_voltage)
print(line_current)

time.sleep(2.0)
time.sleep(2.0)

0 comments on commit cf73188

Please sign in to comment.