A library for ADS1015 and ADS1115 from Texas Instruments.
Fork of ADS1015 by
v1.3 - Added ADS1115_REG_CONFIG_DR & m_dataRate Jul 21. 2014 - Corrected m_conversionDelay - Now there is readADC() that returns counts and readADC_V() that returns voltage
Diff: Adafruit_ADS1015.cpp
- Revision:
- 1:a628fdaed351
- Parent:
- 0:8174d9ceeca1
--- a/Adafruit_ADS1015.cpp Sun Nov 10 18:22:27 2013 +0000 +++ b/Adafruit_ADS1015.cpp Wed May 21 13:12:24 2014 +0000 @@ -17,8 +17,10 @@ @section HISTORY - v1.0 - First release - v1.1.1 - Ported to mbed + v1.0 - First release + v1.1 - Added ADS1115 support - W. Earl + v1.1.1 - Ported to mbed - Arve Seljebu + v1.2 - Fixed error in readADC_SingleEnded() sign bit - Sam Berjawi */ /**************************************************************************/ @@ -106,7 +108,8 @@ @brief Gets a single-ended ADC reading from the specified channel */ /**************************************************************************/ -uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) { +//uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) { SWB +int16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) { if (channel > 3) { return 0; @@ -151,7 +154,27 @@ // 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 readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; SWB + + // SWB + // Shift 12-bit results right 4 bits for the ADS1015 + 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; + } } /**************************************************************************/