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