From 2a7968068fb3ec2ad537d568a97ffd5c7b5f806c Mon Sep 17 00:00:00 2001 From: caternuson Date: Fri, 26 Mar 2021 10:26:13 -0700 Subject: [PATCH] add volts and other tweaks --- Adafruit_ADS1X15.cpp | 68 +++++++++++++++++----------- Adafruit_ADS1X15.h | 3 +- examples/singleended/singleended.ino | 21 ++++++--- keywords.txt | 5 +- library.properties | 2 +- 5 files changed, 62 insertions(+), 37 deletions(-) diff --git a/Adafruit_ADS1X15.cpp b/Adafruit_ADS1X15.cpp index b3407cd..44f5dc0 100644 --- a/Adafruit_ADS1X15.cpp +++ b/Adafruit_ADS1X15.cpp @@ -110,7 +110,7 @@ uint16_t Adafruit_ADS1X15::getDataRate() { return m_dataRate; } @return the ADC reading */ /**************************************************************************/ -uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) { +int16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) { if (channel > 3) { return 0; } @@ -156,8 +156,7 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) { ; // Read the conversion results - // Shift 12-bit results right 4 bits for the ADS1015 - return readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift; + return getLastConversionResults(); } /**************************************************************************/ @@ -199,18 +198,7 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() { ; // Read the conversion results - uint16_t res = readRegister(ADS1X15_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; - } + return getLastConversionResults(); } /**************************************************************************/ @@ -252,18 +240,7 @@ int16_t Adafruit_ADS1X15::readADC_Differential_2_3() { ; // Read the conversion results - uint16_t res = readRegister(ADS1X15_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; - } + return getLastConversionResults(); } /**************************************************************************/ @@ -345,6 +322,43 @@ int16_t Adafruit_ADS1X15::getLastConversionResults() { } } +/**************************************************************************/ +/*! + @brief Returns true if conversion is complete, false otherwise. + + @param counts the ADC reading in raw counts + + @return the ADC reading in volts +*/ +/**************************************************************************/ +float Adafruit_ADS1X15::computeVolts(int16_t counts) { + // see data sheet Table 3 + float fsRange; + switch (m_gain) { + case GAIN_TWOTHIRDS: + fsRange = 6.144f; + break; + case GAIN_ONE: + fsRange = 4.096f; + break; + case GAIN_TWO: + fsRange = 2.048f; + break; + case GAIN_FOUR: + fsRange = 1.024f; + break; + case GAIN_EIGHT: + fsRange = 0.512f; + break; + case GAIN_SIXTEEN: + fsRange = 0.256f; + break; + default: + fsRange = 0.0f; + } + return counts * (fsRange / (32768 >> m_bitShift)); +} + /**************************************************************************/ /*! @brief Returns true if conversion is complete, false otherwise. diff --git a/Adafruit_ADS1X15.h b/Adafruit_ADS1X15.h index 38fa95c..8017838 100644 --- a/Adafruit_ADS1X15.h +++ b/Adafruit_ADS1X15.h @@ -148,11 +148,12 @@ class Adafruit_ADS1X15 { public: void begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire); - uint16_t readADC_SingleEnded(uint8_t channel); + int16_t readADC_SingleEnded(uint8_t channel); int16_t readADC_Differential_0_1(); int16_t readADC_Differential_2_3(); void startComparator_SingleEnded(uint8_t channel, int16_t threshold); int16_t getLastConversionResults(); + float computeVolts(int16_t counts); void setGain(adsGain_t gain); adsGain_t getGain(); void setDataRate(uint16_t rate); diff --git a/examples/singleended/singleended.ino b/examples/singleended/singleended.ino index 94777a4..4b354e5 100644 --- a/examples/singleended/singleended.ino +++ b/examples/singleended/singleended.ino @@ -1,6 +1,6 @@ #include -// Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ +//Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ void setup(void) @@ -30,16 +30,23 @@ void setup(void) void loop(void) { int16_t adc0, adc1, adc2, adc3; + float volts0, volts1, volts2, volts3; adc0 = ads.readADC_SingleEnded(0); adc1 = ads.readADC_SingleEnded(1); adc2 = ads.readADC_SingleEnded(2); adc3 = ads.readADC_SingleEnded(3); - Serial.print("AIN0: "); Serial.println(adc0); - Serial.print("AIN1: "); Serial.println(adc1); - Serial.print("AIN2: "); Serial.println(adc2); - Serial.print("AIN3: "); Serial.println(adc3); - Serial.println(" "); - + + volts0 = ads.computeVolts(adc0); + volts1 = ads.computeVolts(adc1); + volts2 = ads.computeVolts(adc2); + volts3 = ads.computeVolts(adc3); + + Serial.println("-----------------------------------------------------------"); + Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V"); + Serial.print("AIN1: "); Serial.print(adc1); Serial.print(" "); Serial.print(volts1); Serial.println("V"); + Serial.print("AIN2: "); Serial.print(adc2); Serial.print(" "); Serial.print(volts2); Serial.println("V"); + Serial.print("AIN3: "); Serial.print(adc3); Serial.print(" "); Serial.print(volts3); Serial.println("V"); + delay(1000); } diff --git a/keywords.txt b/keywords.txt index c398f40..dccaa45 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,5 +6,8 @@ readADC_Differential_0_1 KEYWORD2 readADC_Differential_2_3 KEYWORD2 startComparator_SingleEnded KEYWORD2 getLastConversionResults KEYWORD2 +computeVolts KEYWORD2 setGain KEYWORD2 -getGain KEYWORD2 \ No newline at end of file +getGain KEYWORD2 +setDataRate KEYWORD2 +getDataRate KEYWORD2 \ No newline at end of file diff --git a/library.properties b/library.properties index cecf6be..61f62dc 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit ADS1X15 -version=2.0.1 +version=2.1.0 author=Adafruit maintainer=Adafruit sentence=Arduino library for ADS1015/1115 ADCs.