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