Skip to content

Commit

Permalink
Merge pull request #4 from barbudor/pr_add_LSB_constants
Browse files Browse the repository at this point in the history
Add LSB constants
  • Loading branch information
barbudor authored Jan 4, 2021
2 parents 363b400 + bc9fb8c commit 39fe4a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 9 additions & 6 deletions barbudor_ina3221/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@
C_MANUFACTURER_ID = const(0x5449) # "TI"
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

class INA3221:
"""Driver class for Texas Instruments INA3221 3 channel current sensor device"""
Expand Down Expand Up @@ -214,7 +217,7 @@ def shunt_voltage(self, channel=1):
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_signed(self.read(C_REG_SHUNT_VOLTAGE_CH[channel])) / 8.0
# convert to volts - LSB = 40uV
return value * 0.00004
return value * C_SHUNT_ADC_LSB

def current(self, channel=1):
"""Return's the channel current in Amps"""
Expand All @@ -226,30 +229,30 @@ def bus_voltage(self, channel=1):
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_signed(self.read(C_REG_BUS_VOLTAGE_CH[channel])) / 8
# convert to volts - LSB = 8mV
return value * 0.008
return value * C_BUS_ADC_LSB

def shunt_critical_alert_limit(self, channel=1):
"""Returns the channel's shunt voltage critical alert limit in Volts"""
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_signed(self.read(C_REG_CRITICAL_ALERT_LIMIT_CH[channel])) / 8
# convert to volts - LSB = 40uV
return value * 0.00004
return value * C_SHUNT_ADC_LSB

def set_shunt_critical_alert_limit(self, channel, voltage):
"""Sets the channel's shunt voltage critical alert limit in Volts"""
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_unsigned(round(voltage * 0.00004) * 8)
value = self._to_unsigned(round(voltage * C_SHUNT_ADC_LSB) * 8)
self.write(C_REG_CRITICAL_ALERT_LIMIT_CH[channel], value)

def shunt_warning_alert_limit(self, channel=1):
"""Returns the channel's shunt voltage warning alert limit in Volts"""
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_signed(self.read(C_REG_WARNING_ALERT_LIMIT_CH[channel])) / 8
# convert to volts - LSB = 40uV
return value * 0.00004
return value * C_SHUNT_ADC_LSB

def set_shunt_warning_alert_limit(self, channel, voltage):
"""Sets the channel's shunt voltage warning alert limit in Volts"""
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_unsigned(round(voltage * 0.00004) * 8)
value = self._to_unsigned(round(voltage * C_SHUNT_ADC_LSB) * 8)
self.write(C_REG_WARNING_ALERT_LIMIT_CH[channel], value)
8 changes: 6 additions & 2 deletions barbudor_ina3221/lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@
_MANUFACTURER_ID = const(0x5449) # "TI"
_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


class INA3221:
"""Driver class for Texas Instruments INA3221 3 channel current sensor device"""
Expand Down Expand Up @@ -214,7 +218,7 @@ def shunt_voltage(self, channel=1):
#assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
value = self._to_signed(self.read(_REG_SHUNT_VOLTAGE_CH[channel])) / 8.0
# convert to volts - LSB = 40uV
return value * 0.00004
return value * C_SHUNT_ADC_LSB

def current(self, channel=1):
"""Return's the channel current in Amps"""
Expand All @@ -226,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 * 0.008
return value * C_BUS_ADC_LSB

0 comments on commit 39fe4a4

Please sign in to comment.