Skip to content

Commit 97ff764

Browse files
committed
fixed wrong register access for channels 2 and 3
tested with CircuitPython 4.0.0-rc1
1 parent bc5061f commit 97ff764

File tree

3 files changed

+63
-81
lines changed

3 files changed

+63
-81
lines changed

barbudor_ina3221.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
In order to be coherent with the datasheet, the channel index in the below API start at 1.
3535
Value of ``channel`` parameter must be ``1``, ``2`` or ``3``. **Do not use** ``0``
3636
37-
Memory usage (tested with CircuitPython 4.0.0beta5 on CircuitPlayground Express):
37+
Memory usage (tested with CircuitPython 4.0.0-rc.1 on CircuitPlayground Express):
3838
3939
* from barbudor_ina3221 import INA3221 --> 6592 bytes
4040
* ina3221 = INA3221(i2c_bus) --> 112 bytes
@@ -69,9 +69,7 @@
6969
C_REG_CONFIG = const(0x00)
7070

7171
C_RESET = const(0x8000)
72-
C_ENABLE_CH1 = const(0x4000) # default set
73-
C_ENABLE_CH2 = const(0x2000) # default set
74-
C_ENABLE_CH3 = const(0x1000) # default set
72+
C_ENABLE_CH = (None,const(0x4000),const(0x2000),const(0x1000)) # default set
7573

7674
C_AVERAGING_MASK = const(0x0E00)
7775
C_AVERAGING_NONE = const(0x0000) # 1 sample, default
@@ -114,35 +112,21 @@
114112
C_MODE_SHUNT_AND_BUS_CONTINOUS = const(0x0007) # Shunt and bus, continuous (default)
115113

116114
# Other registers
117-
C_REG_SHUNT_VOLTAGE_CH1 = const(0x01)
118-
C_REG_BUS_VOLTAGE_CH1 = const(0x02)
119-
C_REG_SHUNT_VOLTAGE_CH2 = const(0x03)
120-
C_REG_BUS_VOLTAGE_CH2 = const(0x04)
121-
C_REG_SHUNT_VOLTAGE_CH3 = const(0x05)
122-
C_REG_BUS_VOLTAGE_CH3 = const(0x06)
123-
C_REG_CRITICAL_ALERT_LIMIT_CH1 = const(0x07)
124-
C_REG_WARNING_ALERT_LIMIT_CH1 = const(0x08)
125-
C_REG_CRITICAL_ALERT_LIMIT_CH2 = const(0x09)
126-
C_REG_WARNING_ALERT_LIMIT_CH2 = const(0x0A)
127-
C_REG_CRITICAL_ALERT_LIMIT_CH3 = const(0x0B)
128-
C_REG_WARNING_ALERT_LIMIT_CH3 = const(0x0C)
115+
C_REG_SHUNT_VOLTAGE_CH = (None, const(0x01), const(0x03), const(0x05))
116+
C_REG_BUS_VOLTAGE_CH = (None, const(0x02), const(0x04), const(0x06))
117+
C_REG_CRITICAL_ALERT_LIMIT_CH = (None, const(0x07), const(0x09), const(0x0B))
118+
C_REG_WARNING_ALERT_LIMIT_CH = (None, const(0x08), const(0x0A), const(0x0C))
129119
C_REG_SHUNT_VOLTAGE_SUM = const(0x0D)
130120
C_REG_SHUNT_VOLTAGE_SUM_LIMIT = const(0x0E)
131121

132122
# Mask/enable register
133123
C_REG_MASK_ENABLE = const(0x0F)
134-
C_SUM_CONTROL_CH1 = const(0x4000) # default not set
135-
C_SUM_CONTROL_CH2 = const(0x2000) # default not set
136-
C_SUM_CONTROL_CH3 = const(0x1000) # default not set
124+
C_SUM_CONTROL_CH = (None,const(0x4000),const(0x2000),const(0x1000)) # def. not set
137125
C_WARNING_LATCH_ENABLE = const(0x0800) # default not set
138126
C_CRITICAL_LATCH_ENABLE = const(0x0400) # default not set
139-
C_CRITICAL_FLAG_CH1 = const(0x0200)
140-
C_CRITICAL_FLAG_CH2 = const(0x0100)
141-
C_CRITICAL_FLAG_CH3 = const(0x0080)
127+
C_CRITICAL_FLAG_CH = (None,const(0x0200),const(0x0100),const(0x0080))
142128
C_SUM_ALERT_FLAG = const(0x0040)
143-
C_WARNING_FLAG_CH1 = const(0x0020)
144-
C_WARNING_FLAG_CH2 = const(0x0010)
145-
C_WARNING_FLAG_CH3 = const(0x0008)
129+
C_WARNING_FLAG_CH = (None,const(0x0020),const(0x0010),const(0x0008))
146130
C_POWER_ALERT_FLAG = const(0x0004)
147131
C_TIMING_ALERT_FLAG = const(0x0002)
148132
C_CONV_READY_FLAG = const(0x0001)
@@ -209,12 +193,14 @@ def __init__(self, i2c_bus, i2c_addr = _DEFAULT_ADDRESS, shunt_resistor = (0.1,
209193

210194
def is_channel_enabled(self, channel=1):
211195
"""Returns if a given channel is enabled or not"""
212-
bit = C_ENABLE_CH1 >> (channel-1)
196+
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
197+
bit = C_ENABLE_CH[channel]
213198
return self.read(C_REG_CONFIG) & bit != 0
214199

215200
def enable_channel(self, channel=1, enable=True):
216201
"""Enables or disable a given channel"""
217-
bit = C_ENABLE_CH1 >> (channel-1)
202+
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
203+
bit = C_ENABLE_CH[channel]
218204
value = 0
219205
if enable:
220206
value = bit
@@ -223,7 +209,7 @@ def enable_channel(self, channel=1, enable=True):
223209
def shunt_voltage(self, channel=1):
224210
"""Returns the channel's shunt voltage in Volts"""
225211
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
226-
value = self._to_signed(self.read(C_REG_SHUNT_VOLTAGE_CH1 + channel-1)) / 8.0
212+
value = self._to_signed(self.read(C_REG_SHUNT_VOLTAGE_CH[channel])) / 8.0
227213
# convert to volts - LSB = 40uV
228214
return value * 0.00004
229215

@@ -235,32 +221,32 @@ def current(self, channel=1):
235221
def bus_voltage(self, channel=1):
236222
"""Returns the channel's bus voltage in Volts"""
237223
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
238-
value = self._to_signed(self.read(C_REG_BUS_VOLTAGE_CH1 + channel-1)) / 8
224+
value = self._to_signed(self.read(C_REG_BUS_VOLTAGE_CH[channel])) / 8
239225
# convert to volts - LSB = 8mV
240226
return value * 0.008
241227

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

249235
def set_shunt_critical_alert_limit(self, channel, voltage):
250236
"""Sets the channel's shunt voltage critical alert limit in Volts"""
251237
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
252238
value = self._to_unsigned(round(voltage * 0.00004) * 8)
253-
self.write(C_REG_CRITICAL_ALERT_LIMIT_CH1 + channel-1, value)
239+
self.write(C_REG_CRITICAL_ALERT_LIMIT_CH[channel], value)
254240

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

262248
def set_shunt_warning_alert_limit(self, channel, voltage):
263249
"""Sets the channel's shunt voltage warning alert limit in Volts"""
264250
assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
265251
value = self._to_unsigned(round(voltage * 0.00004) * 8)
266-
self.write(C_REG_WARNING_ALERT_LIMIT_CH1 + channel-1, value)
252+
self.write(C_REG_WARNING_ALERT_LIMIT_CH[channel], value)

barbudor_ina3221_lite.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
In order to be coherent with the datasheet, the channel index in the below API start at 1.
3535
Value of ``channel`` parameter must be ``1``, ``2`` or ``3``. **Do not use** ``0``
3636
37-
Memory usage (tested with CircuitPython 4.0.0beta5 on CircuitPlayground Express):
37+
Memory usage (tested with CircuitPython 4.0.0-rc.1 on CircuitPlayground Express):
3838
39-
* from barbudor_ina3221 import INA3221 --> 4560 bytes
39+
* from barbudor_ina3221 import INA3221 --> 4080 bytes
4040
* ina3221 = INA3221(i2c_bus) --> 112 bytes
4141
4242
**Hardware:**
@@ -69,9 +69,7 @@
6969
_REG_CONFIG = const(0x00)
7070

7171
_RESET = const(0x8000)
72-
_ENABLE_CH1 = const(0x4000) # default set
73-
_ENABLE_CH2 = const(0x2000) # default set
74-
_ENABLE_CH3 = const(0x1000) # default set
72+
_ENABLE_CH = (None,const(0x4000),const(0x2000),const(0x1000)) # default set
7573

7674
_AVERAGING_MASK = const(0x0E00)
7775
_AVERAGING_NONE = const(0x0000) # 1 sample, default
@@ -114,35 +112,21 @@
114112
_MODE_SHUNT_AND_BUS_CONTINOUS = const(0x0007) # Shunt and bus, continuous (default)
115113

116114
# Other registers
117-
_REG_SHUNT_VOLTAGE_CH1 = const(0x01)
118-
_REG_BUS_VOLTAGE_CH1 = const(0x02)
119-
_REG_SHUNT_VOLTAGE_CH2 = const(0x03)
120-
_REG_BUS_VOLTAGE_CH2 = const(0x04)
121-
_REG_SHUNT_VOLTAGE_CH3 = const(0x05)
122-
_REG_BUS_VOLTAGE_CH3 = const(0x06)
123-
_REG_CRITICAL_ALERT_LIMIT_CH1 = const(0x07)
124-
_REG_WARNING_ALERT_LIMIT_CH1 = const(0x08)
125-
_REG_CRITICAL_ALERT_LIMIT_CH2 = const(0x09)
126-
_REG_WARNING_ALERT_LIMIT_CH2 = const(0x0A)
127-
_REG_CRITICAL_ALERT_LIMIT_CH3 = const(0x0B)
128-
_REG_WARNING_ALERT_LIMIT_CH3 = const(0x0C)
115+
_REG_SHUNT_VOLTAGE_CH = (None, const(0x01), const(0x03), const(0x05))
116+
_REG_BUS_VOLTAGE_CH = (None, const(0x02), const(0x04), const(0x06))
117+
_REG_CRITICAL_ALERT_LIMIT_CH = (None, const(0x07), const(0x09), const(0x0B))
118+
_REG_WARNING_ALERT_LIMIT_CH = (None, const(0x08), const(0x0A), const(0x0C))
129119
_REG_SHUNT_VOLTAGE_SUM = const(0x0D)
130120
_REG_SHUNT_VOLTAGE_SUM_LIMIT = const(0x0E)
131121

132122
# Mask/enable register
133123
_REG_MASK_ENABLE = const(0x0F)
134-
_SUM_CONTROL_CH1 = const(0x4000) # default not set
135-
_SUM_CONTROL_CH2 = const(0x2000) # default not set
136-
_SUM_CONTROL_CH3 = const(0x1000) # default not set
124+
_SUM_CONTROL_CH = (None,const(0x4000),const(0x2000),const(0x1000)) #default not set
137125
_WARNING_LATCH_ENABLE = const(0x0800) # default not set
138126
_CRITICAL_LATCH_ENABLE = const(0x0400) # default not set
139-
_CRITICAL_FLAG_CH1 = const(0x0200)
140-
_CRITICAL_FLAG_CH2 = const(0x0100)
141-
_CRITICAL_FLAG_CH3 = const(0x0080)
127+
_CRITICAL_FLAG_CH = (None,const(0x0200),const(0x0100),const(0x0080))
142128
_SUM_ALERT_FLAG = const(0x0040)
143-
_WARNING_FLAG_CH1 = const(0x0020)
144-
_WARNING_FLAG_CH2 = const(0x0010)
145-
_WARNING_FLAG_CH3 = const(0x0008)
129+
_WARNING_FLAG_CH = (None,const(0x0020),const(0x0010),const(0x0008))
146130
_POWER_ALERT_FLAG = const(0x0004)
147131
_TIMING_ALERT_FLAG = const(0x0002)
148132
_CONV_READY_FLAG = const(0x0001)
@@ -209,12 +193,14 @@ def __init__(self, i2c_bus, i2c_addr = _DEFAULT_ADDRESS, shunt_resistor = (0.1,
209193

210194
def is_channel_enabled(self, channel=1):
211195
"""Returns if a given channel is enabled or not"""
212-
bit = _ENABLE_CH1 >> (channel-1)
196+
#assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
197+
bit = _ENABLE_CH[channel]
213198
return self.read(_REG_CONFIG) & bit != 0
214199

215200
def enable_channel(self, channel=1, enable=True):
216201
"""Enables or disable a given channel"""
217-
bit = _ENABLE_CH1 >> (channel-1)
202+
#assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
203+
bit = _ENABLE_CH[channel]
218204
value = 0
219205
if enable:
220206
value = bit
@@ -223,7 +209,7 @@ def enable_channel(self, channel=1, enable=True):
223209
def shunt_voltage(self, channel=1):
224210
"""Returns the channel's shunt voltage in Volts"""
225211
#assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
226-
value = self._to_signed(self.read(_REG_SHUNT_VOLTAGE_CH1 + channel-1)) / 8.0
212+
value = self._to_signed(self.read(_REG_SHUNT_VOLTAGE_CH[channel])) / 8.0
227213
# convert to volts - LSB = 40uV
228214
return value * 0.00004
229215

@@ -235,6 +221,6 @@ def current(self, channel=1):
235221
def bus_voltage(self, channel=1):
236222
"""Returns the channel's bus voltage in Volts"""
237223
#assert 1 <= channel <= 3, "channel argument must be 1, 2, or 3"
238-
value = self._to_signed(self.read(_REG_BUS_VOLTAGE_CH1 + channel-1)) / 8
224+
value = self._to_signed(self.read(_REG_BUS_VOLTAGE_CH[channel])) / 8
239225
# convert to volts - LSB = 8mV
240226
return value * 0.008

examples/ina3221_simpletest.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,41 @@
22

33
import time
44
import board
5-
from barbudor_ina3221 import INA3221
6-
7-
# To enable relay on pin A1 to switch-on the test-load
8-
#import digitalio
9-
#pin = digitalio.DigitalInOut(board.A1)
10-
#pin.switch_to_output()
5+
from barbudor_ina3221_lite import INA3221
116

127
i2c_bus = board.I2C()
138
ina3221 = INA3221(i2c_bus)
149

1510
ina3221.enable_channel(1)
11+
ina3221.enable_channel(2)
12+
ina3221.enable_channel(3)
1613

1714
while True:
1815

1916
print("------------------------------")
20-
for chan in (1, 2, 3):
17+
line_title= "Measurement "
18+
line_psu_voltage= "PSU voltage "
19+
line_load_voltage= "Load voltage "
20+
line_shunt_voltage= "Shunt voltage "
21+
line_current= "Current "
22+
23+
for chan in range(1,4):
2124
if ina3221.is_channel_enabled(chan):
22-
print("Channel %d" % chan)
23-
bus_voltage = ina3221.bus_voltage(chan)
24-
shunt_voltage = ina3221.shunt_voltage(chan)
25-
current = ina3221.current(chan)
26-
print(" PSU Voltage: {:6.3f} V".format(bus_voltage + shunt_voltage))
27-
print(" Shunt Voltage: {:9.6f} V".format(shunt_voltage))
28-
print(" Load Voltage: {:6.3f} V".format(bus_voltage))
29-
print(" Current: {:9.6f} A".format(current))
30-
print("")
31-
32-
time.sleep(2.0)
25+
#
26+
bus_voltage= ina3221.bus_voltage(chan)
27+
shunt_voltage= ina3221.shunt_voltage(chan)
28+
current= ina3221.current(chan)
29+
#
30+
line_title+= "| Chan#{:d} ".format(chan)
31+
line_psu_voltage+= "| {:6.3f} V ".format(bus_voltage + shunt_voltage)
32+
line_load_voltage+= "| {:6.3f} V ".format(bus_voltage)
33+
line_shunt_voltage+= "| {:9.6f} V ".format(shunt_voltage)
34+
line_current+= "| {:9.6f} A ".format(current)
35+
36+
print(line_title)
37+
print(line_psu_voltage)
38+
print(line_load_voltage)
39+
print(line_shunt_voltage)
40+
print(line_current)
41+
42+
time.sleep(2.0)

0 commit comments

Comments
 (0)