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