Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added comparator example

  • Loading branch information...
commit 30d3ad72ec3c96fd58e99d9b3218cbcbe10794ea 1 parent e2a5e4c
K.Townsend authored
View
64 Adafruit_ADS1015.cpp
@@ -220,3 +220,67 @@ int16_t Adafruit_ADS1015::readADC_Differential_2_3() {
// Read the conversion results
return (int16_t)(readRegister(ADS1015_REG_POINTER_CONVERT) >> 4);
}
+
+/**************************************************************************/
+/*!
+ @brief Sets up the comparator to operate in basic mode, causing the
+ ALERT/RDY pin to assert (go from high to low) when the ADC
+ value exceeds the specified threshold.
+
+ This will also set the ADC in continuous conversion mode.
+*/
+/**************************************************************************/
+void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
+{
+ uint16_t value;
+
+ // Start with default values
+ uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
+ ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode
+ ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
+ ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
+ ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
+ ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
+ ADS1015_REG_CONFIG_PGA_6_144V | // +/- 6.144V range (limited to VDD +0.3V max!)
+ ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
+
+ // Set single-ended input channel
+ switch (channel)
+ {
+ case (0):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
+ break;
+ case (1):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
+ break;
+ case (2):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
+ break;
+ case (3):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
+ break;
+ }
+
+ // Set the high threshold register
+ writeRegister(ADS1015_REG_POINTER_HITHRESH, threshold << 4);
+
+ // Write config register to the ADC
+ writeRegister(ADS1015_REG_POINTER_CONFIG, config);
+}
+
+/**************************************************************************/
+/*!
+ @brief In order to clear the comparator, we need to read the
+ conversion results. This function reads the last conversion
+ results without changing the config value.
+*/
+/**************************************************************************/
+int16_t Adafruit_ADS1015::getLastConversionResults()
+{
+ // Wait for the conversion to complete
+ delay(1);
+
+ // Read the conversion results
+ return (int16_t)(readRegister(ADS1015_REG_POINTER_CONVERT) >> 4);
+}
+
View
2  Adafruit_ADS1015.h
@@ -106,6 +106,8 @@ class Adafruit_ADS1015{
uint16_t readADC_SingleEnded(uint8_t channel);
int16_t readADC_Differential_0_1(void);
int16_t readADC_Differential_2_3(void);
+ void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
+ int16_t getLastConversionResults();
private:
};
View
29 examples/comparator/comparator.pde
@@ -0,0 +1,29 @@
+#include <Wire.h>
+#include <Adafruit_ADS1015.h>
+
+Adafruit_ADS1015 ads1015;
+
+void setup(void)
+{
+ Serial.begin(9600);
+ Serial.println("Hello!");
+
+ Serial.println("Single-ended readings from AIN0 with >3.0V comparator");
+ Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV)");
+ Serial.println("Comparator Threshold: 1000 (3.000V)");
+ ads1015.begin();
+
+ // Setup 3V comparator on channel 0
+ ads1015.startComparator_SingleEnded(0, 1000);
+}
+
+void loop(void)
+{
+ int16_t adc0;
+
+ // Comparator will only de-assert after a read
+ adc0 = ads1015.getLastConversionResults();
+ Serial.print("AIN0: "); Serial.println(adc0);
+
+ delay(100);
+}
View
2  examples/singleended/singleended.pde
@@ -9,7 +9,7 @@ void setup(void)
Serial.println("Hello!");
Serial.println("Getting single-ended readings from AIN0..3");
- Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV");
+ Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV)");
ads1015.begin();
}
Please sign in to comment.
Something went wrong with that request. Please try again.