A library for ADS1015 and ADS1115 from Texas Instruments.

Dependents:   IGGE_Power ADS1015-hello_world ADS1115-hello_world frdm_rtos_Eth ... more

Use like this

#include "mbed.h"
#include "Adafruit_ADS1015.h"
#include "USBSerial.h"

#define SERIAL_BAUD_RATE    9600

I2C i2c(p23, p18);
Adafruit_ADS1015 ads(&i2c);
USBSerial pc; // USB CDC serial port

 
int main() {
    uint16_t reading;
    while (1) {
        reading = ads.readADC_SingleEnded(0); // read channel 0
        pc.printf("reading: %d\r\n", reading); // print reading    
        wait(2); // loop 2 sek
    }
}

Library ported from Arduino at github repository.

Breakout boards from Adafruit: http://learn.adafruit.com/adafruit-4-channel-adc-breakouts/overview

GitHub repository: https://github.com/adafruit/Adafruit_ADS1X15

Committer:
arve0
Date:
Sat Oct 11 08:44:20 2014 +0000
Revision:
4:6445a678d458
merged ads1115 changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arve0 4:6445a678d458 1 /**************************************************************************/
arve0 4:6445a678d458 2 /*!
arve0 4:6445a678d458 3 @file Adafruit_ADS1015.cpp
arve0 4:6445a678d458 4 @author K.Townsend (Adafruit Industries)
arve0 4:6445a678d458 5 @license BSD (see LICENSE.txt)
arve0 4:6445a678d458 6
arve0 4:6445a678d458 7 Ported to mbed by Arve Seljebu - arve0.github.io
arve0 4:6445a678d458 8
arve0 4:6445a678d458 9 Driver for the ADS1015/ADS1115 ADC
arve0 4:6445a678d458 10
arve0 4:6445a678d458 11 This is a library for the Adafruit MPL115A2 breakout
arve0 4:6445a678d458 12 ----> https://www.adafruit.com/products/1083
arve0 4:6445a678d458 13
arve0 4:6445a678d458 14 Adafruit invests time and resources providing this open source code,
arve0 4:6445a678d458 15 please support Adafruit and open-source hardware by purchasing
arve0 4:6445a678d458 16 products from Adafruit!
arve0 4:6445a678d458 17
arve0 4:6445a678d458 18 @section HISTORY
arve0 4:6445a678d458 19
arve0 4:6445a678d458 20 v1.0 - First release
arve0 4:6445a678d458 21 v1.1.1 - Ported to mbed
arve0 4:6445a678d458 22 */
arve0 4:6445a678d458 23 /**************************************************************************/
arve0 4:6445a678d458 24
arve0 4:6445a678d458 25 #include <mbed.h>
arve0 4:6445a678d458 26 #include "Adafruit_ADS1015.h"
arve0 4:6445a678d458 27
arve0 4:6445a678d458 28 /**************************************************************************/
arve0 4:6445a678d458 29 /*!
arve0 4:6445a678d458 30 @brief Writes 16-bits to the specified destination register
arve0 4:6445a678d458 31 */
arve0 4:6445a678d458 32 /**************************************************************************/
arve0 4:6445a678d458 33 void Adafruit_ADS1015::writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value)
arve0 4:6445a678d458 34 {
arve0 4:6445a678d458 35 char cmd[3];
arve0 4:6445a678d458 36 cmd[0] = (char)reg;
arve0 4:6445a678d458 37 cmd[1] = (char)(value>>8);
arve0 4:6445a678d458 38 cmd[2] = (char)(value & 0xFF);
arve0 4:6445a678d458 39 m_i2c->write(i2cAddress, cmd, 3);
arve0 4:6445a678d458 40 }
arve0 4:6445a678d458 41
arve0 4:6445a678d458 42 /**************************************************************************/
arve0 4:6445a678d458 43 /*!
arve0 4:6445a678d458 44 @brief Reads 16-bits from the specified register
arve0 4:6445a678d458 45 */
arve0 4:6445a678d458 46 /**************************************************************************/
arve0 4:6445a678d458 47 uint16_t Adafruit_ADS1015::readRegister(uint8_t i2cAddress, uint8_t reg)
arve0 4:6445a678d458 48 {
arve0 4:6445a678d458 49 char data[2];
arve0 4:6445a678d458 50 data[0] = reg; // temporary use this to send address to conversion register
arve0 4:6445a678d458 51 m_i2c->write(i2cAddress, data, 1);
arve0 4:6445a678d458 52 m_i2c->read(i2cAddress, data, 2);
arve0 4:6445a678d458 53 return (data[0] << 8 | data [1]);
arve0 4:6445a678d458 54 }
arve0 4:6445a678d458 55
arve0 4:6445a678d458 56 /**************************************************************************/
arve0 4:6445a678d458 57 /*!
arve0 4:6445a678d458 58 @brief Instantiates a new ADS1015 class w/appropriate properties
arve0 4:6445a678d458 59 */
arve0 4:6445a678d458 60 /**************************************************************************/
arve0 4:6445a678d458 61 Adafruit_ADS1015::Adafruit_ADS1015(I2C* i2c, uint8_t i2cAddress)
arve0 4:6445a678d458 62 {
arve0 4:6445a678d458 63 // shift 7 bit address 1 left: read expects 8 bit address, see I2C.h
arve0 4:6445a678d458 64 m_i2cAddress = i2cAddress << 1;
arve0 4:6445a678d458 65 m_conversionDelay = ADS1015_CONVERSIONDELAY;
arve0 4:6445a678d458 66 m_bitShift = 4;
arve0 4:6445a678d458 67 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
arve0 4:6445a678d458 68 m_i2c = i2c;
arve0 4:6445a678d458 69 }
arve0 4:6445a678d458 70
arve0 4:6445a678d458 71 /**************************************************************************/
arve0 4:6445a678d458 72 /*!
arve0 4:6445a678d458 73 @brief Instantiates a new ADS1115 class w/appropriate properties
arve0 4:6445a678d458 74 */
arve0 4:6445a678d458 75 /**************************************************************************/
arve0 4:6445a678d458 76 Adafruit_ADS1115::Adafruit_ADS1115(I2C* i2c, uint8_t i2cAddress)
arve0 4:6445a678d458 77 {
arve0 4:6445a678d458 78 // shift 7 bit address 1 left: read expects 8 bit address, see mbed's I2C.h
arve0 4:6445a678d458 79 m_i2cAddress = i2cAddress << 1;
arve0 4:6445a678d458 80 m_conversionDelay = ADS1115_CONVERSIONDELAY;
arve0 4:6445a678d458 81 m_bitShift = 0;
arve0 4:6445a678d458 82 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
arve0 4:6445a678d458 83 m_i2c = i2c;
arve0 4:6445a678d458 84 }
arve0 4:6445a678d458 85
arve0 4:6445a678d458 86 /**************************************************************************/
arve0 4:6445a678d458 87 /*!
arve0 4:6445a678d458 88 @brief Sets the gain and input voltage range
arve0 4:6445a678d458 89 */
arve0 4:6445a678d458 90 /**************************************************************************/
arve0 4:6445a678d458 91 void Adafruit_ADS1015::setGain(adsGain_t gain)
arve0 4:6445a678d458 92 {
arve0 4:6445a678d458 93 m_gain = gain;
arve0 4:6445a678d458 94 }
arve0 4:6445a678d458 95
arve0 4:6445a678d458 96 /**************************************************************************/
arve0 4:6445a678d458 97 /*!
arve0 4:6445a678d458 98 @brief Gets a gain and input voltage range
arve0 4:6445a678d458 99 */
arve0 4:6445a678d458 100 /**************************************************************************/
arve0 4:6445a678d458 101 adsGain_t Adafruit_ADS1015::getGain()
arve0 4:6445a678d458 102 {
arve0 4:6445a678d458 103 return m_gain;
arve0 4:6445a678d458 104 }
arve0 4:6445a678d458 105
arve0 4:6445a678d458 106 /**************************************************************************/
arve0 4:6445a678d458 107 /*!
arve0 4:6445a678d458 108 @brief Gets a single-ended ADC reading from the specified channel
arve0 4:6445a678d458 109 */
arve0 4:6445a678d458 110 /**************************************************************************/
arve0 4:6445a678d458 111 uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel)
arve0 4:6445a678d458 112 {
arve0 4:6445a678d458 113 if (channel > 3) {
arve0 4:6445a678d458 114 return 0;
arve0 4:6445a678d458 115 }
arve0 4:6445a678d458 116
arve0 4:6445a678d458 117 // Start with default values
arve0 4:6445a678d458 118 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
arve0 4:6445a678d458 119 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
arve0 4:6445a678d458 120 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
arve0 4:6445a678d458 121 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
arve0 4:6445a678d458 122 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
arve0 4:6445a678d458 123 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
arve0 4:6445a678d458 124
arve0 4:6445a678d458 125 // Set PGA/voltage range
arve0 4:6445a678d458 126 config |= m_gain;
arve0 4:6445a678d458 127
arve0 4:6445a678d458 128 // Set single-ended input channel
arve0 4:6445a678d458 129 switch (channel) {
arve0 4:6445a678d458 130 case (0):
arve0 4:6445a678d458 131 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
arve0 4:6445a678d458 132 break;
arve0 4:6445a678d458 133 case (1):
arve0 4:6445a678d458 134 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
arve0 4:6445a678d458 135 break;
arve0 4:6445a678d458 136 case (2):
arve0 4:6445a678d458 137 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
arve0 4:6445a678d458 138 break;
arve0 4:6445a678d458 139 case (3):
arve0 4:6445a678d458 140 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
arve0 4:6445a678d458 141 break;
arve0 4:6445a678d458 142 }
arve0 4:6445a678d458 143
arve0 4:6445a678d458 144 // Set 'start single-conversion' bit
arve0 4:6445a678d458 145 config |= ADS1015_REG_CONFIG_OS_SINGLE;
arve0 4:6445a678d458 146
arve0 4:6445a678d458 147 // Write config register to the ADC
arve0 4:6445a678d458 148 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
arve0 4:6445a678d458 149
arve0 4:6445a678d458 150 // Wait for the conversion to complete
arve0 4:6445a678d458 151 wait_ms(m_conversionDelay);
arve0 4:6445a678d458 152
arve0 4:6445a678d458 153 // Read the conversion results
arve0 4:6445a678d458 154 // Shift 12-bit results right 4 bits for the ADS1015
arve0 4:6445a678d458 155 return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
arve0 4:6445a678d458 156 }
arve0 4:6445a678d458 157
arve0 4:6445a678d458 158 /**************************************************************************/
arve0 4:6445a678d458 159 /*!
arve0 4:6445a678d458 160 @brief Reads the conversion results, measuring the voltage
arve0 4:6445a678d458 161 difference between the P (AIN0) and N (AIN1) input. Generates
arve0 4:6445a678d458 162 a signed value since the difference can be either
arve0 4:6445a678d458 163 positive or negative.
arve0 4:6445a678d458 164 */
arve0 4:6445a678d458 165 /**************************************************************************/
arve0 4:6445a678d458 166 int16_t Adafruit_ADS1015::readADC_Differential_0_1()
arve0 4:6445a678d458 167 {
arve0 4:6445a678d458 168 // Start with default values
arve0 4:6445a678d458 169 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
arve0 4:6445a678d458 170 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
arve0 4:6445a678d458 171 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
arve0 4:6445a678d458 172 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
arve0 4:6445a678d458 173 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
arve0 4:6445a678d458 174 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
arve0 4:6445a678d458 175
arve0 4:6445a678d458 176 // Set PGA/voltage range
arve0 4:6445a678d458 177 config |= m_gain;
arve0 4:6445a678d458 178
arve0 4:6445a678d458 179 // Set channels
arve0 4:6445a678d458 180 config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
arve0 4:6445a678d458 181
arve0 4:6445a678d458 182 // Set 'start single-conversion' bit
arve0 4:6445a678d458 183 config |= ADS1015_REG_CONFIG_OS_SINGLE;
arve0 4:6445a678d458 184
arve0 4:6445a678d458 185 // Write config register to the ADC
arve0 4:6445a678d458 186 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
arve0 4:6445a678d458 187
arve0 4:6445a678d458 188 // Wait for the conversion to complete
arve0 4:6445a678d458 189 wait_ms(m_conversionDelay);
arve0 4:6445a678d458 190
arve0 4:6445a678d458 191 // Read the conversion results
arve0 4:6445a678d458 192 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
arve0 4:6445a678d458 193 if (m_bitShift == 0) {
arve0 4:6445a678d458 194 return (int16_t)res;
arve0 4:6445a678d458 195 } else {
arve0 4:6445a678d458 196 // Shift 12-bit results right 4 bits for the ADS1015,
arve0 4:6445a678d458 197 // making sure we keep the sign bit intact
arve0 4:6445a678d458 198 if (res > 0x07FF) {
arve0 4:6445a678d458 199 // negative number - extend the sign to 16th bit
arve0 4:6445a678d458 200 res |= 0xF000;
arve0 4:6445a678d458 201 }
arve0 4:6445a678d458 202 return (int16_t)res;
arve0 4:6445a678d458 203 }
arve0 4:6445a678d458 204 }
arve0 4:6445a678d458 205
arve0 4:6445a678d458 206 /**************************************************************************/
arve0 4:6445a678d458 207 /*!
arve0 4:6445a678d458 208 @brief Reads the conversion results, measuring the voltage
arve0 4:6445a678d458 209 difference between the P (AIN2) and N (AIN3) input. Generates
arve0 4:6445a678d458 210 a signed value since the difference can be either
arve0 4:6445a678d458 211 positive or negative.
arve0 4:6445a678d458 212 */
arve0 4:6445a678d458 213 /**************************************************************************/
arve0 4:6445a678d458 214 int16_t Adafruit_ADS1015::readADC_Differential_2_3()
arve0 4:6445a678d458 215 {
arve0 4:6445a678d458 216 // Start with default values
arve0 4:6445a678d458 217 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
arve0 4:6445a678d458 218 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
arve0 4:6445a678d458 219 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
arve0 4:6445a678d458 220 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
arve0 4:6445a678d458 221 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
arve0 4:6445a678d458 222 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
arve0 4:6445a678d458 223
arve0 4:6445a678d458 224 // Set PGA/voltage range
arve0 4:6445a678d458 225 config |= m_gain;
arve0 4:6445a678d458 226
arve0 4:6445a678d458 227 // Set channels
arve0 4:6445a678d458 228 config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N
arve0 4:6445a678d458 229
arve0 4:6445a678d458 230 // Set 'start single-conversion' bit
arve0 4:6445a678d458 231 config |= ADS1015_REG_CONFIG_OS_SINGLE;
arve0 4:6445a678d458 232
arve0 4:6445a678d458 233 // Write config register to the ADC
arve0 4:6445a678d458 234 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
arve0 4:6445a678d458 235
arve0 4:6445a678d458 236 // Wait for the conversion to complete
arve0 4:6445a678d458 237 wait_ms(m_conversionDelay);
arve0 4:6445a678d458 238
arve0 4:6445a678d458 239 // Read the conversion results
arve0 4:6445a678d458 240 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
arve0 4:6445a678d458 241 if (m_bitShift == 0) {
arve0 4:6445a678d458 242 return (int16_t)res;
arve0 4:6445a678d458 243 } else {
arve0 4:6445a678d458 244 // Shift 12-bit results right 4 bits for the ADS1015,
arve0 4:6445a678d458 245 // making sure we keep the sign bit intact
arve0 4:6445a678d458 246 if (res > 0x07FF) {
arve0 4:6445a678d458 247 // negative number - extend the sign to 16th bit
arve0 4:6445a678d458 248 res |= 0xF000;
arve0 4:6445a678d458 249 }
arve0 4:6445a678d458 250 return (int16_t)res;
arve0 4:6445a678d458 251 }
arve0 4:6445a678d458 252 }
arve0 4:6445a678d458 253
arve0 4:6445a678d458 254 /**************************************************************************/
arve0 4:6445a678d458 255 /*!
arve0 4:6445a678d458 256 @brief Sets up the comparator to operate in basic mode, causing the
arve0 4:6445a678d458 257 ALERT/RDY pin to assert (go from high to low) when the ADC
arve0 4:6445a678d458 258 value exceeds the specified threshold.
arve0 4:6445a678d458 259
arve0 4:6445a678d458 260 This will also set the ADC in continuous conversion mode.
arve0 4:6445a678d458 261 */
arve0 4:6445a678d458 262 /**************************************************************************/
arve0 4:6445a678d458 263 void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
arve0 4:6445a678d458 264 {
arve0 4:6445a678d458 265 // Start with default values
arve0 4:6445a678d458 266 uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
arve0 4:6445a678d458 267 ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode
arve0 4:6445a678d458 268 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
arve0 4:6445a678d458 269 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
arve0 4:6445a678d458 270 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
arve0 4:6445a678d458 271 ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
arve0 4:6445a678d458 272 ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
arve0 4:6445a678d458 273
arve0 4:6445a678d458 274 // Set PGA/voltage range
arve0 4:6445a678d458 275 config |= m_gain;
arve0 4:6445a678d458 276
arve0 4:6445a678d458 277 // Set single-ended input channel
arve0 4:6445a678d458 278 switch (channel) {
arve0 4:6445a678d458 279 case (0):
arve0 4:6445a678d458 280 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
arve0 4:6445a678d458 281 break;
arve0 4:6445a678d458 282 case (1):
arve0 4:6445a678d458 283 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
arve0 4:6445a678d458 284 break;
arve0 4:6445a678d458 285 case (2):
arve0 4:6445a678d458 286 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
arve0 4:6445a678d458 287 break;
arve0 4:6445a678d458 288 case (3):
arve0 4:6445a678d458 289 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
arve0 4:6445a678d458 290 break;
arve0 4:6445a678d458 291 }
arve0 4:6445a678d458 292
arve0 4:6445a678d458 293 // Set the high threshold register
arve0 4:6445a678d458 294 // Shift 12-bit results left 4 bits for the ADS1015
arve0 4:6445a678d458 295 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift);
arve0 4:6445a678d458 296
arve0 4:6445a678d458 297 // Write config register to the ADC
arve0 4:6445a678d458 298 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
arve0 4:6445a678d458 299 }
arve0 4:6445a678d458 300
arve0 4:6445a678d458 301 /**************************************************************************/
arve0 4:6445a678d458 302 /*!
arve0 4:6445a678d458 303 @brief In order to clear the comparator, we need to read the
arve0 4:6445a678d458 304 conversion results. This function reads the last conversion
arve0 4:6445a678d458 305 results without changing the config value.
arve0 4:6445a678d458 306 */
arve0 4:6445a678d458 307 /**************************************************************************/
arve0 4:6445a678d458 308 int16_t Adafruit_ADS1015::getLastConversionResults()
arve0 4:6445a678d458 309 {
arve0 4:6445a678d458 310 // Wait for the conversion to complete
arve0 4:6445a678d458 311 wait_ms(m_conversionDelay);
arve0 4:6445a678d458 312
arve0 4:6445a678d458 313 // Read the conversion results
arve0 4:6445a678d458 314 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
arve0 4:6445a678d458 315 if (m_bitShift == 0) {
arve0 4:6445a678d458 316 return (int16_t)res;
arve0 4:6445a678d458 317 } else {
arve0 4:6445a678d458 318 // Shift 12-bit results right 4 bits for the ADS1015,
arve0 4:6445a678d458 319 // making sure we keep the sign bit intact
arve0 4:6445a678d458 320 if (res > 0x07FF) {
arve0 4:6445a678d458 321 // negative number - extend the sign to 16th bit
arve0 4:6445a678d458 322 res |= 0xF000;
arve0 4:6445a678d458 323 }
arve0 4:6445a678d458 324 return (int16_t)res;
arve0 4:6445a678d458 325 }
arve0 4:6445a678d458 326 }