Skip to content

Commit ba65120

Browse files
committed
Added write_config function, updated comparator example
1 parent 8845b0f commit ba65120

File tree

3 files changed

+29
-48
lines changed

3 files changed

+29
-48
lines changed

adafruit_ads1x15/ads1x15.py

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,12 @@ def comparator_latch(self, comp_latch: int) -> None:
309309
raise ValueError("Unsupported mode.")
310310
self._comparator_latch = comp_latch
311311

312-
def read(self, pin: Pin, is_differential: bool = False) -> int:
312+
def read(self, pin: Pin) -> int:
313313
"""I2C Interface for ADS1x15-based ADCs reads.
314314
315315
:param ~microcontroller.Pin pin: individual or differential pin.
316316
:param bool is_differential: single-ended or differential read.
317317
"""
318-
pin = pin if is_differential else pin + 0x04
319318
return self._read(pin)
320319

321320
def _data_rate_default(self) -> int:
@@ -342,19 +341,7 @@ def _read(self, pin: Pin) -> int:
342341

343342
# Configure ADC every time before a conversion in SINGLE mode
344343
# or changing channels in CONTINUOUS mode
345-
if self.mode == Mode.SINGLE:
346-
config = _ADS1X15_CONFIG_OS_SINGLE
347-
else:
348-
config = 0
349-
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
350-
config |= _ADS1X15_CONFIG_GAIN[self.gain]
351-
config |= self.mode
352-
config |= self.rate_config[self.data_rate]
353-
config |= self.comparator_mode
354-
config |= self.comparator_polarity
355-
config |= self.comparator_latch
356-
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.comparator_queue_length]
357-
self._write_register(_ADS1X15_POINTER_CONFIG, config)
344+
self.write_config(pin)
358345

359346
# Wait for conversion to complete
360347
# ADS1x1x devices settle within a single conversion cycle
@@ -403,34 +390,21 @@ def _read_register(self, reg: int, fast: bool = False) -> int:
403390
i2c.write_then_readinto(bytearray([reg]), self.buf, in_end=2)
404391
return self.buf[0] << 8 | self.buf[1]
405392

406-
def read_config(self) -> None:
407-
"""Reads Config Register and sets all properties accordingly"""
408-
config_value = self._read_register(_ADS1X15_POINTER_CONFIG)
409-
410-
self.gain = next(
411-
key
412-
for key, value in _ADS1X15_CONFIG_GAIN.items()
413-
if value == (config_value & 0x0E00)
414-
)
415-
self.data_rate = next(
416-
key
417-
for key, value in self.rate_config.items()
418-
if value == (config_value & 0x00E0)
419-
)
420-
self.comparator_queue_length = next(
421-
key
422-
for key, value in _ADS1X15_CONFIG_COMP_QUEUE.items()
423-
if value == (config_value & 0x0003)
424-
)
425-
self.mode = Mode.SINGLE if config_value & 0x0100 else Mode.CONTINUOUS
426-
self.comparator_mode = (
427-
Comp_Mode.WINDOW if config_value & 0x0010 else Comp_Mode.TRADITIONAL
428-
)
429-
self.comparator_polarity = (
430-
Comp_Polarity.ACTIVE_HIGH
431-
if config_value & 0x0008
432-
else Comp_Polarity.ACTIVE_LOW
433-
)
434-
self.comparator_latch = (
435-
Comp_Latch.LATCHING if config_value & 0x0004 else Comp_Latch.NONLATCHING
436-
)
393+
def write_config(self, pin_config: int) -> None:
394+
"""Write to configuration register of ADC
395+
396+
:param int pin_config: setting for MUX value in config register
397+
"""
398+
if self.mode == Mode.SINGLE:
399+
config = _ADS1X15_CONFIG_OS_SINGLE
400+
else:
401+
config = 0
402+
config |= (pin_config & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
403+
config |= _ADS1X15_CONFIG_GAIN[self.gain]
404+
config |= self.mode
405+
config |= self.rate_config[self.data_rate]
406+
config |= self.comparator_mode
407+
config |= self.comparator_polarity
408+
config |= self.comparator_latch
409+
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.comparator_queue_length]
410+
self._write_register(_ADS1X15_POINTER_CONFIG, config)

adafruit_ads1x15/analog_in.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ def value(self) -> int:
5555
Even if the underlying analog to digital converter (ADC) is
5656
lower resolution, the value is 16-bit.
5757
"""
58-
return self._ads.read(self._pin_setting, is_differential=self.is_differential)
58+
pin = self._pin_setting if self.is_differential else self._pin_setting + 0x04
59+
return self._ads.read(pin)
60+
61+
def write_config(self) -> None:
62+
"""Calculates MUX setting and writes to configuration register"""
63+
pin = self._pin_setting if self.is_differential else self._pin_setting + 0x04
64+
self._ads.write_config(pin)
5965

6066
@property
6167
def voltage(self) -> float:

examples/ads1x15_comparator_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
ads.comparator_latch = Comp_Latch.NONLATCHING
3838
# Set comparator output to logic LOW when asserted
3939
ads.comparator_polarity = Comp_Polarity.ACTIVE_LOW
40-
4140
# Gain should be explicitly set to ensure threshold values are calculated correctly
4241
ads.gain = 1
4342
# Set comparator low threshold to 2V
4443
ads.comparator_low_threshold = chan.convert_to_value(2.000)
4544
# Set comparator high threshold to 2.002V. High threshold must be above low threshold
4645
ads.comparator_high_threshold = chan.convert_to_value(2.002)
4746

47+
chan.write_config()
48+
4849
count = 0
4950
while True:
5051
print(chan.value, chan.voltage) # This initiates new ADC reading

0 commit comments

Comments
 (0)