Library for the ADS1015 and ADS1115 ADCS

Fork of ADS1015 by Momo-Medical

Revision:
9:fc08929a1093
Parent:
8:bf2e64504294
diff -r bf2e64504294 -r fc08929a1093 Adafruit_ADS1015.cpp
--- a/Adafruit_ADS1015.cpp	Mon Jun 12 09:50:50 2017 +0000
+++ b/Adafruit_ADS1015.cpp	Tue Mar 06 15:47:44 2018 +0000
@@ -148,12 +148,12 @@
     writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
 
     // Wait for the conversion to complete
-    //wait_us(m_conversionDelay);
+    wait_us(m_conversionDelay);
 
     // Read the conversion results
     // Shift 12-bit results right 4 bits for the ADS1015
-    //return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
-    return 1;
+    return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
+//    return 1;
 }
 
 /**************************************************************************/
@@ -164,21 +164,21 @@
             positive or negative.
 */
 /**************************************************************************/
-int16_t Adafruit_ADS1015::readADC_Differential_0_1()
+int16_t Adafruit_ADS1015::readADC_Differential_0_3()
 {
     // Start with default values
     uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE    | // Disable the comparator (default val)
                       ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
                       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(ADS1015) or 250(ADS1115) samples per second (default)
+                      ADS1015_REG_CONFIG_DR_3300SPS   | // 1600(ADS1015) or 250(ADS1115) samples per second (default)
                       ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)
 
     // Set PGA/voltage range
     config |= m_gain;
 
     // Set channels
-    config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1;          // AIN0 = P, AIN1 = N
+    config |= ADS1015_REG_CONFIG_MUX_DIFF_0_3;          // AIN0 = P, AIN1 = N
 
     // Set 'start single-conversion' bit
     config |= ADS1015_REG_CONFIG_OS_SINGLE;
@@ -187,7 +187,55 @@
     writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
 
     // Wait for the conversion to complete
-    wait_ms(m_conversionDelay);
+    wait_us(m_conversionDelay);
+
+    // Read the conversion results
+    uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
+    if (m_bitShift == 0) {
+        return (int16_t)res;
+    } else {
+        // Shift 12-bit results right 4 bits for the ADS1015,
+        // making sure we keep the sign bit intact
+        if (res > 0x07FF) {
+            // negative number - extend the sign to 16th bit
+            res |= 0xF000;
+        }
+        return (int16_t)res;
+    }
+}
+
+/**************************************************************************/
+/*!
+    @brief  Reads the conversion results, measuring the voltage
+            difference between the P (AIN0) and N (AIN1) input.  Generates
+            a signed value since the difference can be either
+            positive or negative.
+*/
+/**************************************************************************/
+int16_t Adafruit_ADS1015::readADC_Differential_1_3()
+{
+    // Start with default values
+    uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE    | // Disable the comparator (default val)
+                      ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
+                      ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
+                      ADS1015_REG_CONFIG_CMODE_TRAD   | // Traditional comparator (default val)
+                      ADS1015_REG_CONFIG_DR_3300SPS   | // 1600(ADS1015) or 250(ADS1115) samples per second (default)
+                      ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)
+
+    // Set PGA/voltage range
+    config |= m_gain;
+
+    // Set channels
+    config |= ADS1015_REG_CONFIG_MUX_DIFF_1_3;          // AIN0 = P, AIN1 = N
+
+    // Set 'start single-conversion' bit
+    config |= ADS1015_REG_CONFIG_OS_SINGLE;
+
+    // Write config register to the ADC
+    writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
+
+    // Wait for the conversion to complete
+    wait_us(m_conversionDelay);
 
     // Read the conversion results
     uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
@@ -219,7 +267,7 @@
                       ADS1015_REG_CONFIG_CLAT_NONLAT  | // Non-latching (default val)
                       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(ADS1015) or 250(ADS1115) samples per second (default)
+                      ADS1015_REG_CONFIG_DR_3300SPS   | // 1600(ADS1015) or 250(ADS1115) samples per second (default)
                       ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)
 
     // Set PGA/voltage range
@@ -235,7 +283,7 @@
     writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
 
     // Wait for the conversion to complete
-    wait_ms(m_conversionDelay);
+    wait_us(m_conversionDelay);
 
     // Read the conversion results
     uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;