Skip to content

Commit

Permalink
Added write_config function, updated comparator example
Browse files Browse the repository at this point in the history
  • Loading branch information
RoaCode committed Aug 12, 2024
1 parent 8845b0f commit ba65120
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
66 changes: 20 additions & 46 deletions adafruit_ads1x15/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,12 @@ def comparator_latch(self, comp_latch: int) -> None:
raise ValueError("Unsupported mode.")
self._comparator_latch = comp_latch

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

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

# Configure ADC every time before a conversion in SINGLE mode
# or changing channels in CONTINUOUS mode
if self.mode == Mode.SINGLE:
config = _ADS1X15_CONFIG_OS_SINGLE
else:
config = 0
config |= (pin & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
config |= _ADS1X15_CONFIG_GAIN[self.gain]
config |= self.mode
config |= self.rate_config[self.data_rate]
config |= self.comparator_mode
config |= self.comparator_polarity
config |= self.comparator_latch
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.comparator_queue_length]
self._write_register(_ADS1X15_POINTER_CONFIG, config)
self.write_config(pin)

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

def read_config(self) -> None:
"""Reads Config Register and sets all properties accordingly"""
config_value = self._read_register(_ADS1X15_POINTER_CONFIG)

self.gain = next(
key
for key, value in _ADS1X15_CONFIG_GAIN.items()
if value == (config_value & 0x0E00)
)
self.data_rate = next(
key
for key, value in self.rate_config.items()
if value == (config_value & 0x00E0)
)
self.comparator_queue_length = next(
key
for key, value in _ADS1X15_CONFIG_COMP_QUEUE.items()
if value == (config_value & 0x0003)
)
self.mode = Mode.SINGLE if config_value & 0x0100 else Mode.CONTINUOUS
self.comparator_mode = (
Comp_Mode.WINDOW if config_value & 0x0010 else Comp_Mode.TRADITIONAL
)
self.comparator_polarity = (
Comp_Polarity.ACTIVE_HIGH
if config_value & 0x0008
else Comp_Polarity.ACTIVE_LOW
)
self.comparator_latch = (
Comp_Latch.LATCHING if config_value & 0x0004 else Comp_Latch.NONLATCHING
)
def write_config(self, pin_config: int) -> None:
"""Write to configuration register of ADC
:param int pin_config: setting for MUX value in config register
"""
if self.mode == Mode.SINGLE:
config = _ADS1X15_CONFIG_OS_SINGLE
else:
config = 0
config |= (pin_config & 0x07) << _ADS1X15_CONFIG_MUX_OFFSET
config |= _ADS1X15_CONFIG_GAIN[self.gain]
config |= self.mode
config |= self.rate_config[self.data_rate]
config |= self.comparator_mode
config |= self.comparator_polarity
config |= self.comparator_latch
config |= _ADS1X15_CONFIG_COMP_QUEUE[self.comparator_queue_length]
self._write_register(_ADS1X15_POINTER_CONFIG, config)
8 changes: 7 additions & 1 deletion adafruit_ads1x15/analog_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def value(self) -> int:
Even if the underlying analog to digital converter (ADC) is
lower resolution, the value is 16-bit.
"""
return self._ads.read(self._pin_setting, is_differential=self.is_differential)
pin = self._pin_setting if self.is_differential else self._pin_setting + 0x04
return self._ads.read(pin)

def write_config(self) -> None:
"""Calculates MUX setting and writes to configuration register"""
pin = self._pin_setting if self.is_differential else self._pin_setting + 0x04
self._ads.write_config(pin)

@property
def voltage(self) -> float:
Expand Down
3 changes: 2 additions & 1 deletion examples/ads1x15_comparator_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
ads.comparator_latch = Comp_Latch.NONLATCHING
# Set comparator output to logic LOW when asserted
ads.comparator_polarity = Comp_Polarity.ACTIVE_LOW

# Gain should be explicitly set to ensure threshold values are calculated correctly
ads.gain = 1
# Set comparator low threshold to 2V
ads.comparator_low_threshold = chan.convert_to_value(2.000)
# Set comparator high threshold to 2.002V. High threshold must be above low threshold
ads.comparator_high_threshold = chan.convert_to_value(2.002)

chan.write_config()

count = 0
while True:
print(chan.value, chan.voltage) # This initiates new ADC reading
Expand Down

0 comments on commit ba65120

Please sign in to comment.