Library for the ADS1015 and ADS1115 ADCS
Fork of ADS1015 by
Adafruit_ADS1015.cpp@18:4b4e3446cdd4, 2018-06-21 (annotated)
- Committer:
- xorjoep
- Date:
- Thu Jun 21 11:58:17 2018 +0000
- Revision:
- 18:4b4e3446cdd4
- Parent:
- 16:a9e5edacbaf7
- Parent:
- 15:e659b715ed89
- Child:
- 20:277793ce35b2
idonteven
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 | |
xorjoep | 16:a9e5edacbaf7 | 106 | int16_t * Adafruit_ADS1015::readADC_Differential_DoubleEnded(){ |
xorjoep | 16:a9e5edacbaf7 | 107 | |
xorjoep | 16:a9e5edacbaf7 | 108 | int16_t temp_res; |
xorjoep | 16:a9e5edacbaf7 | 109 | int16_t results[2]; |
xorjoep | 16:a9e5edacbaf7 | 110 | |
xorjoep | 16:a9e5edacbaf7 | 111 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
xorjoep | 16:a9e5edacbaf7 | 112 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
xorjoep | 16:a9e5edacbaf7 | 113 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
xorjoep | 16:a9e5edacbaf7 | 114 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
xorjoep | 16:a9e5edacbaf7 | 115 | ADS1015_REG_CONFIG_DR_3300SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
xorjoep | 16:a9e5edacbaf7 | 116 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
xorjoep | 16:a9e5edacbaf7 | 117 | |
xorjoep | 16:a9e5edacbaf7 | 118 | // Set PGA/voltage range |
xorjoep | 16:a9e5edacbaf7 | 119 | config |= m_gain; |
xorjoep | 16:a9e5edacbaf7 | 120 | |
xorjoep | 16:a9e5edacbaf7 | 121 | // Set channels |
xorjoep | 16:a9e5edacbaf7 | 122 | config |= ADS1015_REG_CONFIG_MUX_DIFF_0_3; // AIN0 = P, AIN1 = N |
xorjoep | 16:a9e5edacbaf7 | 123 | |
xorjoep | 16:a9e5edacbaf7 | 124 | // Set 'start single-conversion' bit |
xorjoep | 16:a9e5edacbaf7 | 125 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
xorjoep | 16:a9e5edacbaf7 | 126 | |
xorjoep | 16:a9e5edacbaf7 | 127 | // Read the first |
xorjoep | 16:a9e5edacbaf7 | 128 | temp_res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
xorjoep | 16:a9e5edacbaf7 | 129 | |
xorjoep | 16:a9e5edacbaf7 | 130 | if (m_bitShift != 0) { |
xorjoep | 16:a9e5edacbaf7 | 131 | // Shift 12-bit results right 4 bits for the ADS1015, |
xorjoep | 16:a9e5edacbaf7 | 132 | // making sure we keep the sign bit intact |
xorjoep | 16:a9e5edacbaf7 | 133 | if (temp_res > 0x07FF) { |
xorjoep | 16:a9e5edacbaf7 | 134 | // negative number - extend the sign to 16th bit |
xorjoep | 16:a9e5edacbaf7 | 135 | temp_res |= 0xF000; |
xorjoep | 16:a9e5edacbaf7 | 136 | } |
xorjoep | 16:a9e5edacbaf7 | 137 | } |
xorjoep | 16:a9e5edacbaf7 | 138 | results[0] = temp_res; |
xorjoep | 16:a9e5edacbaf7 | 139 | |
xorjoep | 16:a9e5edacbaf7 | 140 | |
xorjoep | 16:a9e5edacbaf7 | 141 | // Write config register to the ADC |
xorjoep | 16:a9e5edacbaf7 | 142 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
xorjoep | 16:a9e5edacbaf7 | 143 | |
xorjoep | 16:a9e5edacbaf7 | 144 | // Wait for the conversion to complete |
xorjoep | 16:a9e5edacbaf7 | 145 | wait_us(m_conversionDelay); |
xorjoep | 16:a9e5edacbaf7 | 146 | |
xorjoep | 16:a9e5edacbaf7 | 147 | // Read the conversion results |
xorjoep | 16:a9e5edacbaf7 | 148 | temp_res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
xorjoep | 16:a9e5edacbaf7 | 149 | |
xorjoep | 16:a9e5edacbaf7 | 150 | if (m_bitShift != 0) { |
xorjoep | 16:a9e5edacbaf7 | 151 | // Shift 12-bit results right 4 bits for the ADS1015, |
xorjoep | 16:a9e5edacbaf7 | 152 | // making sure we keep the sign bit intact |
xorjoep | 16:a9e5edacbaf7 | 153 | if (temp_res > 0x07FF) { |
xorjoep | 16:a9e5edacbaf7 | 154 | // negative number - extend the sign to 16th bit |
xorjoep | 16:a9e5edacbaf7 | 155 | temp_res |= 0xF000; |
xorjoep | 16:a9e5edacbaf7 | 156 | } |
xorjoep | 16:a9e5edacbaf7 | 157 | } |
xorjoep | 16:a9e5edacbaf7 | 158 | results[1] |= temp_res; |
xorjoep | 16:a9e5edacbaf7 | 159 | |
xorjoep | 16:a9e5edacbaf7 | 160 | config |= ADS1015_REG_CONFIG_MUX_DIFF_1_3; |
xorjoep | 16:a9e5edacbaf7 | 161 | |
xorjoep | 16:a9e5edacbaf7 | 162 | // Write config register to the ADC |
xorjoep | 16:a9e5edacbaf7 | 163 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
xorjoep | 16:a9e5edacbaf7 | 164 | |
xorjoep | 16:a9e5edacbaf7 | 165 | return results; |
xorjoep | 16:a9e5edacbaf7 | 166 | } |
xorjoep | 16:a9e5edacbaf7 | 167 | |
arve0 | 0:8174d9ceeca1 | 168 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 169 | /*! |
arve0 | 0:8174d9ceeca1 | 170 | @brief Gets a single-ended ADC reading from the specified channel |
arve0 | 0:8174d9ceeca1 | 171 | */ |
arve0 | 0:8174d9ceeca1 | 172 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 173 | uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) |
arve0 | 3:fb735fb343de | 174 | { |
arve0 | 3:fb735fb343de | 175 | if (channel > 3) { |
arve0 | 3:fb735fb343de | 176 | return 0; |
arve0 | 3:fb735fb343de | 177 | } |
arve0 | 0:8174d9ceeca1 | 178 | |
arve0 | 3:fb735fb343de | 179 | // Start with default values |
arve0 | 3:fb735fb343de | 180 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
arve0 | 3:fb735fb343de | 181 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
arve0 | 3:fb735fb343de | 182 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 183 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
DEldering | 6:e6fecdc96fb1 | 184 | ADS1015_REG_CONFIG_DR_3300SPS | // 860(ADS1115) samples per second |
arve0 | 3:fb735fb343de | 185 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
arve0 | 3:fb735fb343de | 186 | |
arve0 | 3:fb735fb343de | 187 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 188 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 189 | |
arve0 | 3:fb735fb343de | 190 | // Set single-ended input channel |
arve0 | 3:fb735fb343de | 191 | switch (channel) { |
arve0 | 3:fb735fb343de | 192 | case (0): |
arve0 | 3:fb735fb343de | 193 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; |
arve0 | 3:fb735fb343de | 194 | break; |
arve0 | 3:fb735fb343de | 195 | case (1): |
arve0 | 3:fb735fb343de | 196 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; |
arve0 | 3:fb735fb343de | 197 | break; |
arve0 | 3:fb735fb343de | 198 | case (2): |
arve0 | 3:fb735fb343de | 199 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; |
arve0 | 3:fb735fb343de | 200 | break; |
arve0 | 3:fb735fb343de | 201 | case (3): |
arve0 | 3:fb735fb343de | 202 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; |
arve0 | 3:fb735fb343de | 203 | break; |
arve0 | 3:fb735fb343de | 204 | } |
arve0 | 0:8174d9ceeca1 | 205 | |
arve0 | 3:fb735fb343de | 206 | // Set 'start single-conversion' bit |
arve0 | 3:fb735fb343de | 207 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
arve0 | 0:8174d9ceeca1 | 208 | |
arve0 | 3:fb735fb343de | 209 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 210 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 0:8174d9ceeca1 | 211 | |
arve0 | 3:fb735fb343de | 212 | // Wait for the conversion to complete |
DEldering | 10:da5d0da65437 | 213 | wait_us(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 214 | |
arve0 | 3:fb735fb343de | 215 | // Read the conversion results |
arve0 | 3:fb735fb343de | 216 | // Shift 12-bit results right 4 bits for the ADS1015 |
DEldering | 10:da5d0da65437 | 217 | return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
DEldering | 10:da5d0da65437 | 218 | // return 1; |
arve0 | 0:8174d9ceeca1 | 219 | } |
arve0 | 0:8174d9ceeca1 | 220 | |
arve0 | 0:8174d9ceeca1 | 221 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 222 | /*! |
arve0 | 0:8174d9ceeca1 | 223 | @brief Reads the conversion results, measuring the voltage |
arve0 | 0:8174d9ceeca1 | 224 | difference between the P (AIN0) and N (AIN1) input. Generates |
arve0 | 0:8174d9ceeca1 | 225 | a signed value since the difference can be either |
arve0 | 0:8174d9ceeca1 | 226 | positive or negative. |
arve0 | 0:8174d9ceeca1 | 227 | */ |
arve0 | 0:8174d9ceeca1 | 228 | /**************************************************************************/ |
xorjoep | 13:e0d3c07d79fa | 229 | int16_t Adafruit_ADS1015::readADC_Differential_0_3() |
arve0 | 3:fb735fb343de | 230 | { |
arve0 | 3:fb735fb343de | 231 | // Start with default values |
arve0 | 3:fb735fb343de | 232 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
arve0 | 3:fb735fb343de | 233 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
arve0 | 3:fb735fb343de | 234 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 235 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
xorjoep | 14:e8dd6dcf64e9 | 236 | ADS1015_REG_CONFIG_DR_3300SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 237 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
arve0 | 0:8174d9ceeca1 | 238 | |
arve0 | 3:fb735fb343de | 239 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 240 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 241 | |
arve0 | 3:fb735fb343de | 242 | // Set channels |
xorjoep | 13:e0d3c07d79fa | 243 | config |= ADS1015_REG_CONFIG_MUX_DIFF_0_3; // AIN0 = P, AIN1 = N |
arve0 | 0:8174d9ceeca1 | 244 | |
arve0 | 3:fb735fb343de | 245 | // Set 'start single-conversion' bit |
arve0 | 3:fb735fb343de | 246 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
arve0 | 0:8174d9ceeca1 | 247 | |
arve0 | 3:fb735fb343de | 248 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 249 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 3:fb735fb343de | 250 | |
arve0 | 3:fb735fb343de | 251 | // Wait for the conversion to complete |
deldering95 | 12:b8485d232bf8 | 252 | wait_us(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 253 | |
arve0 | 3:fb735fb343de | 254 | // Read the conversion results |
arve0 | 3:fb735fb343de | 255 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 3:fb735fb343de | 256 | if (m_bitShift == 0) { |
arve0 | 3:fb735fb343de | 257 | return (int16_t)res; |
arve0 | 3:fb735fb343de | 258 | } else { |
arve0 | 3:fb735fb343de | 259 | // Shift 12-bit results right 4 bits for the ADS1015, |
arve0 | 3:fb735fb343de | 260 | // making sure we keep the sign bit intact |
arve0 | 3:fb735fb343de | 261 | if (res > 0x07FF) { |
arve0 | 3:fb735fb343de | 262 | // negative number - extend the sign to 16th bit |
arve0 | 3:fb735fb343de | 263 | res |= 0xF000; |
arve0 | 3:fb735fb343de | 264 | } |
arve0 | 3:fb735fb343de | 265 | return (int16_t)res; |
arve0 | 0:8174d9ceeca1 | 266 | } |
arve0 | 0:8174d9ceeca1 | 267 | } |
arve0 | 0:8174d9ceeca1 | 268 | |
arve0 | 0:8174d9ceeca1 | 269 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 270 | /*! |
arve0 | 0:8174d9ceeca1 | 271 | @brief Reads the conversion results, measuring the voltage |
arve0 | 0:8174d9ceeca1 | 272 | difference between the P (AIN2) and N (AIN3) input. Generates |
arve0 | 0:8174d9ceeca1 | 273 | a signed value since the difference can be either |
arve0 | 0:8174d9ceeca1 | 274 | positive or negative. |
arve0 | 0:8174d9ceeca1 | 275 | */ |
arve0 | 0:8174d9ceeca1 | 276 | /**************************************************************************/ |
xorjoep | 13:e0d3c07d79fa | 277 | int16_t Adafruit_ADS1015::readADC_Differential_1_3() |
arve0 | 3:fb735fb343de | 278 | { |
arve0 | 3:fb735fb343de | 279 | // Start with default values |
arve0 | 3:fb735fb343de | 280 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
arve0 | 3:fb735fb343de | 281 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
arve0 | 3:fb735fb343de | 282 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 283 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
xorjoep | 14:e8dd6dcf64e9 | 284 | ADS1015_REG_CONFIG_DR_3300SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 285 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
arve0 | 0:8174d9ceeca1 | 286 | |
arve0 | 3:fb735fb343de | 287 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 288 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 289 | |
arve0 | 3:fb735fb343de | 290 | // Set channels |
xorjoep | 13:e0d3c07d79fa | 291 | config |= ADS1015_REG_CONFIG_MUX_DIFF_1_3; // AIN0 = P, AIN1 = N |
arve0 | 0:8174d9ceeca1 | 292 | |
arve0 | 3:fb735fb343de | 293 | // Set 'start single-conversion' bit |
arve0 | 3:fb735fb343de | 294 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
arve0 | 0:8174d9ceeca1 | 295 | |
arve0 | 3:fb735fb343de | 296 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 297 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 3:fb735fb343de | 298 | |
arve0 | 3:fb735fb343de | 299 | // Wait for the conversion to complete |
xorjoep | 13:e0d3c07d79fa | 300 | wait_us(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 301 | |
arve0 | 3:fb735fb343de | 302 | // Read the conversion results |
arve0 | 3:fb735fb343de | 303 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 3:fb735fb343de | 304 | if (m_bitShift == 0) { |
arve0 | 3:fb735fb343de | 305 | return (int16_t)res; |
arve0 | 3:fb735fb343de | 306 | } else { |
arve0 | 3:fb735fb343de | 307 | // Shift 12-bit results right 4 bits for the ADS1015, |
arve0 | 3:fb735fb343de | 308 | // making sure we keep the sign bit intact |
arve0 | 3:fb735fb343de | 309 | if (res > 0x07FF) { |
arve0 | 3:fb735fb343de | 310 | // negative number - extend the sign to 16th bit |
arve0 | 3:fb735fb343de | 311 | res |= 0xF000; |
arve0 | 3:fb735fb343de | 312 | } |
arve0 | 3:fb735fb343de | 313 | return (int16_t)res; |
arve0 | 0:8174d9ceeca1 | 314 | } |
arve0 | 0:8174d9ceeca1 | 315 | } |
xorjoep | 15:e659b715ed89 | 316 | int16_t * Adafruit_ADS1015::readADC_Differential_DoubleEnded(){ |
xorjoep | 15:e659b715ed89 | 317 | |
xorjoep | 15:e659b715ed89 | 318 | uint16_t temp_res; |
xorjoep | 15:e659b715ed89 | 319 | static int16_t results[2]; |
xorjoep | 15:e659b715ed89 | 320 | |
xorjoep | 15:e659b715ed89 | 321 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
xorjoep | 15:e659b715ed89 | 322 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
xorjoep | 15:e659b715ed89 | 323 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
xorjoep | 15:e659b715ed89 | 324 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
xorjoep | 15:e659b715ed89 | 325 | ADS1015_REG_CONFIG_DR_3300SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
xorjoep | 15:e659b715ed89 | 326 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
xorjoep | 15:e659b715ed89 | 327 | |
xorjoep | 15:e659b715ed89 | 328 | // Set PGA/voltage range |
xorjoep | 15:e659b715ed89 | 329 | config |= m_gain; |
xorjoep | 15:e659b715ed89 | 330 | |
xorjoep | 15:e659b715ed89 | 331 | // Set channels |
xorjoep | 15:e659b715ed89 | 332 | config |= ADS1015_REG_CONFIG_MUX_DIFF_0_3; // AIN0 = P, AIN1 = N |
xorjoep | 15:e659b715ed89 | 333 | |
xorjoep | 15:e659b715ed89 | 334 | // Set 'start single-conversion' bit |
xorjoep | 15:e659b715ed89 | 335 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
xorjoep | 15:e659b715ed89 | 336 | |
xorjoep | 15:e659b715ed89 | 337 | // // Read the first |
xorjoep | 15:e659b715ed89 | 338 | // temp_res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
xorjoep | 15:e659b715ed89 | 339 | // |
xorjoep | 15:e659b715ed89 | 340 | // if (m_bitShift != 0) { |
xorjoep | 15:e659b715ed89 | 341 | // // Shift 12-bit results right 4 bits for the ADS1015, |
xorjoep | 15:e659b715ed89 | 342 | // // making sure we keep the sign bit intact |
xorjoep | 15:e659b715ed89 | 343 | // if (temp_res > 0x07FF) { |
xorjoep | 15:e659b715ed89 | 344 | // // negative number - extend the sign to 16th bit |
xorjoep | 15:e659b715ed89 | 345 | // temp_res |= 0xF000; |
xorjoep | 15:e659b715ed89 | 346 | // } |
xorjoep | 15:e659b715ed89 | 347 | // } |
xorjoep | 15:e659b715ed89 | 348 | // results[0] = temp_res; |
xorjoep | 15:e659b715ed89 | 349 | |
xorjoep | 15:e659b715ed89 | 350 | |
xorjoep | 15:e659b715ed89 | 351 | // Write config register to the ADC |
xorjoep | 15:e659b715ed89 | 352 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
xorjoep | 15:e659b715ed89 | 353 | |
xorjoep | 15:e659b715ed89 | 354 | // Wait for the conversion to complete |
xorjoep | 15:e659b715ed89 | 355 | wait_us(m_conversionDelay); |
xorjoep | 15:e659b715ed89 | 356 | |
xorjoep | 15:e659b715ed89 | 357 | // Read the conversion results |
xorjoep | 15:e659b715ed89 | 358 | temp_res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
xorjoep | 15:e659b715ed89 | 359 | |
xorjoep | 15:e659b715ed89 | 360 | if (m_bitShift != 0) { |
xorjoep | 15:e659b715ed89 | 361 | // Shift 12-bit results right 4 bits for the ADS1015, |
xorjoep | 15:e659b715ed89 | 362 | // making sure we keep the sign bit intact |
xorjoep | 15:e659b715ed89 | 363 | if (temp_res > 0x07FF) { |
xorjoep | 15:e659b715ed89 | 364 | // negative number - extend the sign to 16th bit |
xorjoep | 15:e659b715ed89 | 365 | temp_res |= 0xF000; |
xorjoep | 15:e659b715ed89 | 366 | } |
xorjoep | 15:e659b715ed89 | 367 | } |
xorjoep | 15:e659b715ed89 | 368 | results[1] |= (int16_t) temp_res; |
xorjoep | 15:e659b715ed89 | 369 | |
xorjoep | 15:e659b715ed89 | 370 | //config |= ADS1015_REG_CONFIG_MUX_DIFF_1_3; |
xorjoep | 15:e659b715ed89 | 371 | // |
xorjoep | 15:e659b715ed89 | 372 | // // Write config register to the ADC |
xorjoep | 15:e659b715ed89 | 373 | // writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
xorjoep | 15:e659b715ed89 | 374 | |
xorjoep | 15:e659b715ed89 | 375 | return results; |
xorjoep | 15:e659b715ed89 | 376 | } |
xorjoep | 15:e659b715ed89 | 377 | |
arve0 | 0:8174d9ceeca1 | 378 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 379 | /*! |
arve0 | 0:8174d9ceeca1 | 380 | @brief Sets up the comparator to operate in basic mode, causing the |
arve0 | 0:8174d9ceeca1 | 381 | ALERT/RDY pin to assert (go from high to low) when the ADC |
arve0 | 0:8174d9ceeca1 | 382 | value exceeds the specified threshold. |
arve0 | 0:8174d9ceeca1 | 383 | |
arve0 | 0:8174d9ceeca1 | 384 | This will also set the ADC in continuous conversion mode. |
arve0 | 0:8174d9ceeca1 | 385 | */ |
arve0 | 0:8174d9ceeca1 | 386 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 387 | void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold) |
arve0 | 0:8174d9ceeca1 | 388 | { |
arve0 | 3:fb735fb343de | 389 | // Start with default values |
arve0 | 3:fb735fb343de | 390 | uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match |
arve0 | 3:fb735fb343de | 391 | ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode |
arve0 | 3:fb735fb343de | 392 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 393 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
arve0 | 4:6445a678d458 | 394 | ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 395 | ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode |
arve0 | 3:fb735fb343de | 396 | ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode |
arve0 | 3:fb735fb343de | 397 | |
arve0 | 3:fb735fb343de | 398 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 399 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 400 | |
arve0 | 3:fb735fb343de | 401 | // Set single-ended input channel |
arve0 | 3:fb735fb343de | 402 | switch (channel) { |
arve0 | 3:fb735fb343de | 403 | case (0): |
arve0 | 3:fb735fb343de | 404 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; |
arve0 | 3:fb735fb343de | 405 | break; |
arve0 | 3:fb735fb343de | 406 | case (1): |
arve0 | 3:fb735fb343de | 407 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; |
arve0 | 3:fb735fb343de | 408 | break; |
arve0 | 3:fb735fb343de | 409 | case (2): |
arve0 | 3:fb735fb343de | 410 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; |
arve0 | 3:fb735fb343de | 411 | break; |
arve0 | 3:fb735fb343de | 412 | case (3): |
arve0 | 3:fb735fb343de | 413 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; |
arve0 | 3:fb735fb343de | 414 | break; |
arve0 | 3:fb735fb343de | 415 | } |
arve0 | 0:8174d9ceeca1 | 416 | |
arve0 | 3:fb735fb343de | 417 | // Set the high threshold register |
arve0 | 3:fb735fb343de | 418 | // Shift 12-bit results left 4 bits for the ADS1015 |
arve0 | 3:fb735fb343de | 419 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift); |
arve0 | 0:8174d9ceeca1 | 420 | |
arve0 | 3:fb735fb343de | 421 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 422 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 0:8174d9ceeca1 | 423 | } |
arve0 | 0:8174d9ceeca1 | 424 | |
arve0 | 0:8174d9ceeca1 | 425 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 426 | /*! |
arve0 | 0:8174d9ceeca1 | 427 | @brief In order to clear the comparator, we need to read the |
arve0 | 0:8174d9ceeca1 | 428 | conversion results. This function reads the last conversion |
arve0 | 0:8174d9ceeca1 | 429 | results without changing the config value. |
arve0 | 0:8174d9ceeca1 | 430 | */ |
arve0 | 0:8174d9ceeca1 | 431 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 432 | int16_t Adafruit_ADS1015::getLastConversionResults() |
arve0 | 0:8174d9ceeca1 | 433 | { |
arve0 | 3:fb735fb343de | 434 | // Wait for the conversion to complete |
xorjoep | 15:e659b715ed89 | 435 | wait_us(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 436 | |
arve0 | 3:fb735fb343de | 437 | // Read the conversion results |
arve0 | 3:fb735fb343de | 438 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 3:fb735fb343de | 439 | if (m_bitShift == 0) { |
arve0 | 3:fb735fb343de | 440 | return (int16_t)res; |
arve0 | 3:fb735fb343de | 441 | } else { |
arve0 | 3:fb735fb343de | 442 | // Shift 12-bit results right 4 bits for the ADS1015, |
arve0 | 3:fb735fb343de | 443 | // making sure we keep the sign bit intact |
arve0 | 3:fb735fb343de | 444 | if (res > 0x07FF) { |
arve0 | 3:fb735fb343de | 445 | // negative number - extend the sign to 16th bit |
arve0 | 3:fb735fb343de | 446 | res |= 0xF000; |
arve0 | 3:fb735fb343de | 447 | } |
arve0 | 3:fb735fb343de | 448 | return (int16_t)res; |
arve0 | 0:8174d9ceeca1 | 449 | } |
arve0 | 3:fb735fb343de | 450 | } |