update data rate, jadi 860SPS. Belum ditest
Adafruit_ADS1015.cpp@6:441bc773778a, 2019-02-25 (annotated)
- Committer:
- adavidkhowantolim
- Date:
- Mon Feb 25 15:50:05 2019 +0000
- Revision:
- 6:441bc773778a
- Parent:
- 4:6445a678d458
- Child:
- 7:653f782ce3aa
FIX;
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!) */ |
adavidkhowantolim | 6:441bc773778a | 68 | // m_gain = GAIN_TWO; /* +/- 2.048V range (limited to VDD +0.3V max!) */ |
arve0 | 3:fb735fb343de | 69 | m_i2c = i2c; |
arve0 | 0:8174d9ceeca1 | 70 | } |
arve0 | 0:8174d9ceeca1 | 71 | |
arve0 | 0:8174d9ceeca1 | 72 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 73 | /*! |
arve0 | 0:8174d9ceeca1 | 74 | @brief Instantiates a new ADS1115 class w/appropriate properties |
arve0 | 0:8174d9ceeca1 | 75 | */ |
arve0 | 0:8174d9ceeca1 | 76 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 77 | Adafruit_ADS1115::Adafruit_ADS1115(I2C* i2c, uint8_t i2cAddress) |
arve0 | 0:8174d9ceeca1 | 78 | { |
arve0 | 3:fb735fb343de | 79 | // shift 7 bit address 1 left: read expects 8 bit address, see mbed's I2C.h |
arve0 | 3:fb735fb343de | 80 | m_i2cAddress = i2cAddress << 1; |
arve0 | 3:fb735fb343de | 81 | m_conversionDelay = ADS1115_CONVERSIONDELAY; |
arve0 | 3:fb735fb343de | 82 | m_bitShift = 0; |
arve0 | 3:fb735fb343de | 83 | m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ |
adavidkhowantolim | 6:441bc773778a | 84 | // m_gain = GAIN_TWO; /* +/- 2.048V range (limited to VDD +0.3V max!) */ |
arve0 | 3:fb735fb343de | 85 | m_i2c = i2c; |
arve0 | 0:8174d9ceeca1 | 86 | } |
arve0 | 0:8174d9ceeca1 | 87 | |
arve0 | 0:8174d9ceeca1 | 88 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 89 | /*! |
arve0 | 0:8174d9ceeca1 | 90 | @brief Sets the gain and input voltage range |
arve0 | 0:8174d9ceeca1 | 91 | */ |
arve0 | 0:8174d9ceeca1 | 92 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 93 | void Adafruit_ADS1015::setGain(adsGain_t gain) |
arve0 | 0:8174d9ceeca1 | 94 | { |
arve0 | 3:fb735fb343de | 95 | m_gain = gain; |
arve0 | 0:8174d9ceeca1 | 96 | } |
arve0 | 0:8174d9ceeca1 | 97 | |
arve0 | 0:8174d9ceeca1 | 98 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 99 | /*! |
arve0 | 0:8174d9ceeca1 | 100 | @brief Gets a gain and input voltage range |
arve0 | 0:8174d9ceeca1 | 101 | */ |
arve0 | 0:8174d9ceeca1 | 102 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 103 | adsGain_t Adafruit_ADS1015::getGain() |
arve0 | 0:8174d9ceeca1 | 104 | { |
arve0 | 3:fb735fb343de | 105 | return m_gain; |
arve0 | 0:8174d9ceeca1 | 106 | } |
arve0 | 0:8174d9ceeca1 | 107 | |
arve0 | 0:8174d9ceeca1 | 108 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 109 | /*! |
arve0 | 0:8174d9ceeca1 | 110 | @brief Gets a single-ended ADC reading from the specified channel |
arve0 | 0:8174d9ceeca1 | 111 | */ |
arve0 | 0:8174d9ceeca1 | 112 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 113 | uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) |
arve0 | 3:fb735fb343de | 114 | { |
arve0 | 3:fb735fb343de | 115 | if (channel > 3) { |
arve0 | 3:fb735fb343de | 116 | return 0; |
arve0 | 3:fb735fb343de | 117 | } |
arve0 | 0:8174d9ceeca1 | 118 | |
arve0 | 3:fb735fb343de | 119 | // Start with default values |
arve0 | 3:fb735fb343de | 120 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
arve0 | 3:fb735fb343de | 121 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
arve0 | 3:fb735fb343de | 122 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 123 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
arve0 | 4:6445a678d458 | 124 | ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 125 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
arve0 | 3:fb735fb343de | 126 | |
arve0 | 3:fb735fb343de | 127 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 128 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 129 | |
arve0 | 3:fb735fb343de | 130 | // Set single-ended input channel |
arve0 | 3:fb735fb343de | 131 | switch (channel) { |
arve0 | 3:fb735fb343de | 132 | case (0): |
arve0 | 3:fb735fb343de | 133 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; |
arve0 | 3:fb735fb343de | 134 | break; |
arve0 | 3:fb735fb343de | 135 | case (1): |
arve0 | 3:fb735fb343de | 136 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; |
arve0 | 3:fb735fb343de | 137 | break; |
arve0 | 3:fb735fb343de | 138 | case (2): |
arve0 | 3:fb735fb343de | 139 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; |
arve0 | 3:fb735fb343de | 140 | break; |
arve0 | 3:fb735fb343de | 141 | case (3): |
arve0 | 3:fb735fb343de | 142 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; |
arve0 | 3:fb735fb343de | 143 | break; |
arve0 | 3:fb735fb343de | 144 | } |
arve0 | 0:8174d9ceeca1 | 145 | |
arve0 | 3:fb735fb343de | 146 | // Set 'start single-conversion' bit |
arve0 | 3:fb735fb343de | 147 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
arve0 | 0:8174d9ceeca1 | 148 | |
arve0 | 3:fb735fb343de | 149 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 150 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 0:8174d9ceeca1 | 151 | |
arve0 | 3:fb735fb343de | 152 | // Wait for the conversion to complete |
arve0 | 3:fb735fb343de | 153 | wait_ms(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 154 | |
arve0 | 3:fb735fb343de | 155 | // Read the conversion results |
arve0 | 3:fb735fb343de | 156 | // Shift 12-bit results right 4 bits for the ADS1015 |
arve0 | 3:fb735fb343de | 157 | return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 0:8174d9ceeca1 | 158 | } |
arve0 | 0:8174d9ceeca1 | 159 | |
arve0 | 0:8174d9ceeca1 | 160 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 161 | /*! |
arve0 | 0:8174d9ceeca1 | 162 | @brief Reads the conversion results, measuring the voltage |
arve0 | 0:8174d9ceeca1 | 163 | difference between the P (AIN0) and N (AIN1) input. Generates |
arve0 | 0:8174d9ceeca1 | 164 | a signed value since the difference can be either |
arve0 | 0:8174d9ceeca1 | 165 | positive or negative. |
arve0 | 0:8174d9ceeca1 | 166 | */ |
arve0 | 0:8174d9ceeca1 | 167 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 168 | int16_t Adafruit_ADS1015::readADC_Differential_0_1() |
arve0 | 3:fb735fb343de | 169 | { |
arve0 | 3:fb735fb343de | 170 | // Start with default values |
arve0 | 3:fb735fb343de | 171 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
arve0 | 3:fb735fb343de | 172 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
arve0 | 3:fb735fb343de | 173 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 174 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
arve0 | 4:6445a678d458 | 175 | ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 176 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
arve0 | 0:8174d9ceeca1 | 177 | |
arve0 | 3:fb735fb343de | 178 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 179 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 180 | |
arve0 | 3:fb735fb343de | 181 | // Set channels |
arve0 | 3:fb735fb343de | 182 | config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N |
arve0 | 0:8174d9ceeca1 | 183 | |
arve0 | 3:fb735fb343de | 184 | // Set 'start single-conversion' bit |
arve0 | 3:fb735fb343de | 185 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
arve0 | 0:8174d9ceeca1 | 186 | |
arve0 | 3:fb735fb343de | 187 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 188 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 3:fb735fb343de | 189 | |
arve0 | 3:fb735fb343de | 190 | // Wait for the conversion to complete |
arve0 | 3:fb735fb343de | 191 | wait_ms(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 192 | |
arve0 | 3:fb735fb343de | 193 | // Read the conversion results |
arve0 | 3:fb735fb343de | 194 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 3:fb735fb343de | 195 | if (m_bitShift == 0) { |
arve0 | 3:fb735fb343de | 196 | return (int16_t)res; |
arve0 | 3:fb735fb343de | 197 | } else { |
arve0 | 3:fb735fb343de | 198 | // Shift 12-bit results right 4 bits for the ADS1015, |
arve0 | 3:fb735fb343de | 199 | // making sure we keep the sign bit intact |
arve0 | 3:fb735fb343de | 200 | if (res > 0x07FF) { |
arve0 | 3:fb735fb343de | 201 | // negative number - extend the sign to 16th bit |
arve0 | 3:fb735fb343de | 202 | res |= 0xF000; |
arve0 | 3:fb735fb343de | 203 | } |
arve0 | 3:fb735fb343de | 204 | return (int16_t)res; |
arve0 | 0:8174d9ceeca1 | 205 | } |
arve0 | 0:8174d9ceeca1 | 206 | } |
arve0 | 0:8174d9ceeca1 | 207 | |
arve0 | 0:8174d9ceeca1 | 208 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 209 | /*! |
arve0 | 0:8174d9ceeca1 | 210 | @brief Reads the conversion results, measuring the voltage |
arve0 | 0:8174d9ceeca1 | 211 | difference between the P (AIN2) and N (AIN3) input. Generates |
arve0 | 0:8174d9ceeca1 | 212 | a signed value since the difference can be either |
arve0 | 0:8174d9ceeca1 | 213 | positive or negative. |
arve0 | 0:8174d9ceeca1 | 214 | */ |
arve0 | 0:8174d9ceeca1 | 215 | /**************************************************************************/ |
arve0 | 3:fb735fb343de | 216 | int16_t Adafruit_ADS1015::readADC_Differential_2_3() |
arve0 | 3:fb735fb343de | 217 | { |
arve0 | 3:fb735fb343de | 218 | // Start with default values |
arve0 | 3:fb735fb343de | 219 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
arve0 | 3:fb735fb343de | 220 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
arve0 | 3:fb735fb343de | 221 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 222 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
arve0 | 4:6445a678d458 | 223 | ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 224 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
arve0 | 0:8174d9ceeca1 | 225 | |
arve0 | 3:fb735fb343de | 226 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 227 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 228 | |
arve0 | 3:fb735fb343de | 229 | // Set channels |
arve0 | 3:fb735fb343de | 230 | config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N |
arve0 | 0:8174d9ceeca1 | 231 | |
arve0 | 3:fb735fb343de | 232 | // Set 'start single-conversion' bit |
arve0 | 3:fb735fb343de | 233 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
arve0 | 0:8174d9ceeca1 | 234 | |
arve0 | 3:fb735fb343de | 235 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 236 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 3:fb735fb343de | 237 | |
arve0 | 3:fb735fb343de | 238 | // Wait for the conversion to complete |
arve0 | 3:fb735fb343de | 239 | wait_ms(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 240 | |
arve0 | 3:fb735fb343de | 241 | // Read the conversion results |
arve0 | 3:fb735fb343de | 242 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 3:fb735fb343de | 243 | if (m_bitShift == 0) { |
arve0 | 3:fb735fb343de | 244 | return (int16_t)res; |
arve0 | 3:fb735fb343de | 245 | } else { |
arve0 | 3:fb735fb343de | 246 | // Shift 12-bit results right 4 bits for the ADS1015, |
arve0 | 3:fb735fb343de | 247 | // making sure we keep the sign bit intact |
arve0 | 3:fb735fb343de | 248 | if (res > 0x07FF) { |
arve0 | 3:fb735fb343de | 249 | // negative number - extend the sign to 16th bit |
arve0 | 3:fb735fb343de | 250 | res |= 0xF000; |
arve0 | 3:fb735fb343de | 251 | } |
arve0 | 3:fb735fb343de | 252 | return (int16_t)res; |
arve0 | 0:8174d9ceeca1 | 253 | } |
arve0 | 0:8174d9ceeca1 | 254 | } |
arve0 | 0:8174d9ceeca1 | 255 | |
arve0 | 0:8174d9ceeca1 | 256 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 257 | /*! |
arve0 | 0:8174d9ceeca1 | 258 | @brief Sets up the comparator to operate in basic mode, causing the |
arve0 | 0:8174d9ceeca1 | 259 | ALERT/RDY pin to assert (go from high to low) when the ADC |
arve0 | 0:8174d9ceeca1 | 260 | value exceeds the specified threshold. |
arve0 | 0:8174d9ceeca1 | 261 | |
arve0 | 0:8174d9ceeca1 | 262 | This will also set the ADC in continuous conversion mode. |
arve0 | 0:8174d9ceeca1 | 263 | */ |
arve0 | 0:8174d9ceeca1 | 264 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 265 | void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold) |
arve0 | 0:8174d9ceeca1 | 266 | { |
arve0 | 3:fb735fb343de | 267 | // Start with default values |
arve0 | 3:fb735fb343de | 268 | uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match |
arve0 | 3:fb735fb343de | 269 | ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode |
arve0 | 3:fb735fb343de | 270 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
arve0 | 3:fb735fb343de | 271 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
arve0 | 4:6445a678d458 | 272 | ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
arve0 | 3:fb735fb343de | 273 | ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode |
arve0 | 3:fb735fb343de | 274 | ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode |
arve0 | 3:fb735fb343de | 275 | |
arve0 | 3:fb735fb343de | 276 | // Set PGA/voltage range |
arve0 | 3:fb735fb343de | 277 | config |= m_gain; |
arve0 | 0:8174d9ceeca1 | 278 | |
arve0 | 3:fb735fb343de | 279 | // Set single-ended input channel |
arve0 | 3:fb735fb343de | 280 | switch (channel) { |
arve0 | 3:fb735fb343de | 281 | case (0): |
arve0 | 3:fb735fb343de | 282 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; |
arve0 | 3:fb735fb343de | 283 | break; |
arve0 | 3:fb735fb343de | 284 | case (1): |
arve0 | 3:fb735fb343de | 285 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; |
arve0 | 3:fb735fb343de | 286 | break; |
arve0 | 3:fb735fb343de | 287 | case (2): |
arve0 | 3:fb735fb343de | 288 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; |
arve0 | 3:fb735fb343de | 289 | break; |
arve0 | 3:fb735fb343de | 290 | case (3): |
arve0 | 3:fb735fb343de | 291 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; |
arve0 | 3:fb735fb343de | 292 | break; |
arve0 | 3:fb735fb343de | 293 | } |
arve0 | 0:8174d9ceeca1 | 294 | |
arve0 | 3:fb735fb343de | 295 | // Set the high threshold register |
arve0 | 3:fb735fb343de | 296 | // Shift 12-bit results left 4 bits for the ADS1015 |
arve0 | 3:fb735fb343de | 297 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift); |
arve0 | 0:8174d9ceeca1 | 298 | |
arve0 | 3:fb735fb343de | 299 | // Write config register to the ADC |
arve0 | 3:fb735fb343de | 300 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
arve0 | 0:8174d9ceeca1 | 301 | } |
arve0 | 0:8174d9ceeca1 | 302 | |
arve0 | 0:8174d9ceeca1 | 303 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 304 | /*! |
arve0 | 0:8174d9ceeca1 | 305 | @brief In order to clear the comparator, we need to read the |
arve0 | 0:8174d9ceeca1 | 306 | conversion results. This function reads the last conversion |
arve0 | 0:8174d9ceeca1 | 307 | results without changing the config value. |
arve0 | 0:8174d9ceeca1 | 308 | */ |
arve0 | 0:8174d9ceeca1 | 309 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 310 | int16_t Adafruit_ADS1015::getLastConversionResults() |
arve0 | 0:8174d9ceeca1 | 311 | { |
arve0 | 3:fb735fb343de | 312 | // Wait for the conversion to complete |
arve0 | 3:fb735fb343de | 313 | wait_ms(m_conversionDelay); |
arve0 | 0:8174d9ceeca1 | 314 | |
arve0 | 3:fb735fb343de | 315 | // Read the conversion results |
arve0 | 3:fb735fb343de | 316 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
arve0 | 3:fb735fb343de | 317 | if (m_bitShift == 0) { |
arve0 | 3:fb735fb343de | 318 | return (int16_t)res; |
arve0 | 3:fb735fb343de | 319 | } else { |
arve0 | 3:fb735fb343de | 320 | // Shift 12-bit results right 4 bits for the ADS1015, |
arve0 | 3:fb735fb343de | 321 | // making sure we keep the sign bit intact |
arve0 | 3:fb735fb343de | 322 | if (res > 0x07FF) { |
arve0 | 3:fb735fb343de | 323 | // negative number - extend the sign to 16th bit |
arve0 | 3:fb735fb343de | 324 | res |= 0xF000; |
arve0 | 3:fb735fb343de | 325 | } |
arve0 | 3:fb735fb343de | 326 | return (int16_t)res; |
arve0 | 0:8174d9ceeca1 | 327 | } |
arve0 | 3:fb735fb343de | 328 | } |