Skip to content

Commit f546de7

Browse files
committed
analog in tested, working on all channels
1 parent f14f0e7 commit f546de7

File tree

4 files changed

+65
-41
lines changed

4 files changed

+65
-41
lines changed

adafruit_tla202x/__init__.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ class Mux(CV):
9393

9494
Range.add_values(
9595
(
96-
("RANGE_6_144V", 0x0, 6.144 V", 3),
97-
("RANGE_4_096V", 0x1, 4.096 V", 2),
98-
("RANGE_2_048V", 0x2, 2.048 V", 1),
99-
("RANGE_1_024V", 0x3, 1.024 V", 0.5),
100-
("RANGE_0_512V", 0x4, 0.512 V", 0.25),
101-
("RANGE_0_256V", 0x5, 0.256 V", 0.125),
96+
("RANGE_6_144V", 0x0, 6.144, 3),
97+
("RANGE_4_096V", 0x1, 4.096, 2),
98+
("RANGE_2_048V", 0x2, 2.048, 1),
99+
("RANGE_1_024V", 0x3, 1.024, 0.5),
100+
("RANGE_0_512V", 0x4, 0.512, 0.25),
101+
("RANGE_0_256V", 0x5, 0.256, 0.125),
102102
)
103103
)
104104
DataRate.add_values(
@@ -131,7 +131,7 @@ class Mux(CV):
131131
# GND – 0.3 V < V(AINX) < VDD + 0.3 V
132132

133133

134-
class TLA2024:
134+
class TLA2024: # pylint:disable=too-many-instance-attributes
135135
"""I2C Interface for analog voltage measurements using the TI TLA2024 12-bit 4-channel ADC
136136
137137
params:
@@ -154,8 +154,9 @@ def __init__(self, i2c_bus, address=_TLA_DEFAULT_ADDRESS):
154154
self._last_one_shot = None
155155
self.mode = Mode.CONTINUOUS
156156
self.mux = Mux.MUX_AIN0_GND
157+
# default to widest range and highest sample rate
157158
self.data_rate = DataRate.RATE_3300SPS
158-
self.range = Range.RANGE_2_048V
159+
self.range = Range.RANGE_6_144V
159160

160161
@property
161162
def voltage(self):
@@ -187,7 +188,7 @@ def mode(self):
187188
@mode.setter
188189
def mode(self, mode):
189190
if not Mode.is_valid(mode):
190-
raise AttributeError("``mode`` must be a valid ``Mode``")
191+
raise AttributeError("``mode`` must be a valid Mode")
191192
if mode == Mode.CONTINUOUS: # pylint:disable=no-member
192193
self._mode = mode
193194
return
@@ -199,6 +200,18 @@ def mode(self, mode):
199200

200201
self._last_one_shot = self._read_volts()
201202

203+
@property
204+
def range(self):
205+
"""The measurement range of the ADC, changed by adjusting the Programmable Gain Amplifier
206+
`range` must be an ``adafruit_tla202x.Range``"""
207+
return self._pga
208+
209+
@range.setter
210+
def range(self, measurement_range):
211+
if not Range.is_valid(measurement_range):
212+
raise AttributeError("range must be a valid Range")
213+
self._pga = measurement_range
214+
202215
@property
203216
def data_rate(self):
204217
"""selects the rate at which measurement samples are taken. Must be an
@@ -208,7 +221,7 @@ def data_rate(self):
208221
@data_rate.setter
209222
def data_rate(self, rate):
210223
if not DataRate.is_valid(rate): # pylint:disable=no-member
211-
raise AttributeError("``data_rate`` must be a valid ``DataRate``")
224+
raise AttributeError("``data_rate`` must be a valid DataRate")
212225
self._data_rate = rate
213226

214227
@property
@@ -220,15 +233,17 @@ def mux(self):
220233

221234
@mux.setter
222235
def mux(self, mux_connection):
236+
print("muxin'")
223237
if not Mux.is_valid(mux_connection): # pylint:disable=no-member
224-
raise AttributeError("``mux`` must be a valid ``Mux``")
238+
raise AttributeError("``mux`` must be a valid Mux")
225239
self._mux = mux_connection
226240

227241
def read(self, channel):
228-
"""Switch to the given channel and take a single voltage reading in One Shot mode"""
229-
self.input_channel = channel
242+
"""Switch to the given channel and take a single ADC reading in One Shot mode"""
243+
if not self.input_channel == channel:
244+
self.input_channel = channel
230245
self.mode = Mode.ONE_SHOT # pylint:disable=no-member
231-
return self.voltage
246+
return self._read_adc()
232247

233248
def _read_volts(self):
234249
"""From datasheet:
@@ -240,16 +255,18 @@ def _read_volts(self):
240255
241256
"""
242257

243-
value_lsb = self._read_lsb()
244-
v_fsr = 8192 >> self.range
245-
if self.range == 0:
246-
v_fsr = 6144
247-
v_fsr = float(v_fsr)
248-
converted = (value_lsb / 2047.0) * v_fsr
258+
value_lsb = self._read_adc()
259+
# print("value_lsb:", hex(value_lsb))
260+
# v_fsr = 8192 >> self.range
261+
# if self.range == 0:
262+
# v_fsr = 6144
263+
# v_fsr = float(v_fsr)
264+
# converted = (value_lsb / 2047.0) * v_fsr
249265

250-
return converted / 1000.0
266+
# return converted / 1000.0
267+
return value_lsb * Range.lsb[self.range] / 1000.0
251268

252-
def _read_lsb(self):
269+
def _read_adc(self):
253270
value_lsb = self._raw_adc_read
254271
value_lsb >>= 4
255272

adafruit_tla202x/analog_in.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
2727
* Author(s): Bryan Siepert
2828
"""
29+
from . import Range
2930

3031

3132
class AnalogIn:
@@ -49,8 +50,8 @@ def voltage(self):
4950
raise RuntimeError(
5051
"Underlying ADC does not exist, likely due to callint `deinit`"
5152
)
52-
raw_reading = self._tla.read(self._channel_number)
53-
return ((raw_reading << 4) / 65535) * self._tla.reference_voltage
53+
self._tla.input_channel = self._channel_number
54+
return self._tla.voltage
5455

5556
@property
5657
def value(self):
@@ -72,7 +73,7 @@ def reference_voltage(self):
7273
raise RuntimeError(
7374
"Underlying ADC does not exist, likely due to callint `deinit`"
7475
)
75-
return self._tla.reference_voltage
76+
return Range.string[self._tla.range]
7677

7778
def deinit(self):
7879
"""Release the reference to the TLA202x. Create a new AnalogIn to use it again."""

examples/tla202x_analog_in.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,30 @@
2020

2121
i2c = board.I2C()
2222
tla = TLA.TLA2024(i2c)
23-
24-
tla_in_0 = AnalogIn(tla, TLA.A0)
23+
val_max = (2 ** 15) - 1
24+
pin_0 = AnalogIn(tla, TLA.A0)
25+
pin_1 = AnalogIn(tla, TLA.A1)
26+
pin_2 = AnalogIn(tla, TLA.A2)
27+
pin_3 = AnalogIn(tla, TLA.A3)
28+
analog_ins = [
29+
pin_0,
30+
pin_1,
31+
pin_2,
32+
pin_3,
33+
]
2534
while True:
26-
raw_value = tla_in_0.value
27-
scaled_value = (raw_value / 65535) * tla_in_0.reference_voltage
35+
for a_in in analog_ins:
36+
raw_value = a_in.value
37+
voltage_reference = a_in.reference_voltage
38+
scaled_value = (raw_value / val_max) * voltage_reference
39+
40+
voltage = a_in.voltage
41+
print("Pin 0 ADC value: %d lsb" % (raw_value))
42+
print("Pin 0 Reference Voltage: %0.2fV" % (voltage_reference))
43+
print("Pin 0 Measured Voltage: %0.2fV" % (scaled_value))
2844

29-
voltage = tla_in_0.voltage
45+
print("")
3046

31-
print("Pin 0 Scaled Voltage: %0.2fV" % (scaled_value))
32-
print("Pin 0 Direct Voltage: %0.2fV" % (voltage))
47+
print("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_")
3348
print("")
3449
time.sleep(1)

examples/tla202x_mux_test.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
channel = i
1414
tla.input_channel = channel
1515
print("Channel", channel, ":", tla.voltage)
16-
# Mux.MUX_AIN0_AIN1 X
17-
# Mux.MUX_AIN0_AIN3 X
18-
# Mux.MUX_AIN1_AIN3 X
19-
# Mux.MUX_AIN2_AIN3 X
20-
# Mux.MUX_AIN0_GND X
21-
# Mux.MUX_AIN1_GND X
22-
# Mux.MUX_AIN2_GND X
23-
# Mux.MUX_AIN3_GND X
24-
2516

2617
muxen = [
2718
[Mux.MUX_AIN0_GND, 0.5],

0 commit comments

Comments
 (0)