Skip to content

Commit 33e0755

Browse files
committed
- Fix values for _CONFIG_BADCRES and add missing values
- Add properties (get & set) for config register and config fields break-up
1 parent c57fb34 commit 33e0755

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

adafruit_ina219.py

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,33 @@
5858
_CONFIG_RESET = const(0x8000) # Reset Bit
5959

6060
_CONFIG_BVOLTAGERANGE_MASK = const(0x2000) # Bus Voltage Range Mask
61+
_CONFIG_BVOLTAGERANGE_LSB = const(13) # Bus Voltage Range LSB position
6162
_CONFIG_BVOLTAGERANGE_16V = const(0x0000) # 0-16V Range
6263
_CONFIG_BVOLTAGERANGE_32V = const(0x2000) # 0-32V Range
6364

6465
_CONFIG_GAIN_MASK = const(0x1800) # Gain Mask
66+
_CONFIG_GAIN_LSB = const(11) # Gain LSB position
6567
_CONFIG_GAIN_1_40MV = const(0x0000) # Gain 1, 40mV Range
6668
_CONFIG_GAIN_2_80MV = const(0x0800) # Gain 2, 80mV Range
6769
_CONFIG_GAIN_4_160MV = const(0x1000) # Gain 4, 160mV Range
6870
_CONFIG_GAIN_8_320MV = const(0x1800) # Gain 8, 320mV Range
6971

7072
_CONFIG_BADCRES_MASK = const(0x0780) # Bus ADC Resolution Mask
71-
_CONFIG_BADCRES_9BIT = const(0x0080) # 9-bit bus res = 0..511
72-
_CONFIG_BADCRES_10BIT = const(0x0100) # 10-bit bus res = 0..1023
73-
_CONFIG_BADCRES_11BIT = const(0x0200) # 11-bit bus res = 0..2047
74-
_CONFIG_BADCRES_12BIT = const(0x0400) # 12-bit bus res = 0..4097
73+
_CONFIG_BADCRES_LSB = const(7) # Bus ADC Resolution LSB position
74+
_CONFIG_BADCRES_9BIT = const(0x0000) # 9-bit bus res = 0..511
75+
_CONFIG_BADCRES_10BIT = const(0x0080) # 10-bit bus res = 0..1023
76+
_CONFIG_BADCRES_11BIT = const(0x0100) # 11-bit bus res = 0..2047
77+
_CONFIG_BADCRES_12BIT = const(0x0180) # 12-bit bus res = 0..4097
78+
_CONFIG_BADCRES_12BIT_2S_1060US = const(0x0480) # 2 x 12-bit bus samples averaged together
79+
_CONFIG_BADCRES_12BIT_4S_2130US = const(0x0500) # 4 x 12-bit bus samples averaged together
80+
_CONFIG_BADCRES_12BIT_8S_4260US = const(0x0580) # 8 x 12-bit bus samples averaged together
81+
_CONFIG_BADCRES_12BIT_16S_8510US = const(0x0600) # 16 x 12-bit bus samples averaged together
82+
_CONFIG_BADCRES_12BIT_32S_17MS = const(0x0680) # 32 x 12-bit bus samples averaged together
83+
_CONFIG_BADCRES_12BIT_64S_34MS = const(0x0700) # 64 x 12-bit bus samples averaged together
84+
_CONFIG_BADCRES_12BIT_128S_69MS = const(0x0780) # 128 x 12-bit bus samples averaged together
7585

7686
_CONFIG_SADCRES_MASK = const(0x0078) # Shunt ADC Resolution and Averaging Mask
87+
_CONFIG_SADCRES_LSB = const(3) # Shunt ADC Resolution and Averaging LSB position
7788
_CONFIG_SADCRES_9BIT_1S_84US = const(0x0000) # 1 x 9-bit shunt sample
7889
_CONFIG_SADCRES_10BIT_1S_148US = const(0x0008) # 1 x 10-bit shunt sample
7990
_CONFIG_SADCRES_11BIT_1S_276US = const(0x0010) # 1 x 11-bit shunt sample
@@ -87,6 +98,7 @@
8798
_CONFIG_SADCRES_12BIT_128S_69MS = const(0x0078) # 128 x 12-bit shunt samples averaged together
8899

89100
_CONFIG_MODE_MASK = const(0x0007) # Operating Mode Mask
101+
_CONFIG_MODE_LSB = const(0) # Operating Mode LSB position
90102
_CONFIG_MODE_POWERDOWN = const(0x0000)
91103
_CONFIG_MODE_SVOLT_TRIGGERED = const(0x0001)
92104
_CONFIG_MODE_BVOLT_TRIGGERED = const(0x0002)
@@ -147,6 +159,63 @@ def _read_register(self, reg):
147159
value = (buf[1] << 8) | (buf[2])
148160
return value
149161

162+
@property
163+
def config(self):
164+
return self._read_register(_REG_CONFIG)
165+
166+
@config.setter
167+
def config(self,value):
168+
self._write_register(_REG_CONFIG,value)
169+
170+
@property
171+
def reset(self):
172+
self._write_register(_REG_CONFIG,_CONFIG_RESET)
173+
174+
@property
175+
def bus_voltage_range(self):
176+
return (self.config & _CONFIG_BVOLTAGERANGE_MASK) >> _CONFIG_BVOLTAGERANGE_LSB
177+
178+
@bus_voltage_range.setter
179+
def bus_voltage_range(self,value):
180+
value = (value << _CONFIG_BVOLTAGERANGE_LSB) & _CONFIG_BVOLTAGERANGE_MASK
181+
self.config = value | (self.config & ~_CONFIG_BVOLTAGERANGE_MASK)
182+
183+
@property
184+
def gain(self):
185+
return (self.config & _CONFIG_GAIN_MASK) >> _CONFIG_GAIN_LSB
186+
187+
@gain.setter
188+
def gain(self,value):
189+
value = (value << _CONFIG_GAIN_LSB) & _CONFIG_GAIN_MASK
190+
self.config = value | (self.config & ~_CONFIG_GAIN_MASK)
191+
192+
@property
193+
def bus_adc_res(self):
194+
return (self.config & _CONFIG_BADCRES_MASK) >> _CONFIG_BADCRES_LSB
195+
196+
@bus_adc_res.setter
197+
def bus_adc_res(self,value):
198+
value = (value << _CONFIG_BADCRES_LSB) & _CONFIG_BADCRES_MASK
199+
self.config = value | (self.config & ~_CONFIG_BADCRES_MASK)
200+
201+
@property
202+
def shunt_adc_res(self):
203+
return (self.config & _CONFIG_SADCRES_MASK) >> _CONFIG_SADCRES_LSB
204+
205+
@shunt_adc_res.setter
206+
def shunt_adc_res(self,value):
207+
value = (value << _CONFIG_SADCRES_LSB) & _CONFIG_SADCRES_MASK
208+
self.config = value | (self.config & ~_CONFIG_SADCRES_MASK)
209+
210+
@property
211+
def mode(self):
212+
return (self.config & _CONFIG_MODE_MASK) >> _CONFIG_MODE_LSB
213+
214+
@mode.setter
215+
def mode(self,value):
216+
value = (value << _CONFIG_MODE_LSB) & _CONFIG_MODE_MASK
217+
self.config = value | (self.config & ~_CONFIG_MODE_MASK)
218+
150219
@property
151220
def shunt_voltage(self):
152221
"""The shunt voltage (between V+ and V-) in Volts (so +-.327V)"""

0 commit comments

Comments
 (0)