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