A library for ADS1015 and ADS1115 from Texas Instruments.
Fork of ADS1015 by
v1.3 - Added ADS1115_REG_CONFIG_DR & m_dataRate Jul 21. 2014 - Corrected m_conversionDelay - Now there is readADC() that returns counts and readADC_V() that returns voltage
ADS1x15.cpp@2:976073ea60df, 2014-07-22 (annotated)
- Committer:
- Sambo007
- Date:
- Tue Jul 22 06:04:13 2014 +0000
- Revision:
- 2:976073ea60df
v1.3 - Added ADS1115_REG_CONFIG_DR & m_dataRate - Jul 21. 2014; - Corrected m_conversionDelay; - Now there is readADC() that returns counts and readADC_V() that returns voltage;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sambo007 | 2:976073ea60df | 1 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 2 | /*! |
Sambo007 | 2:976073ea60df | 3 | @file ADS1x15.cpp |
Sambo007 | 2:976073ea60df | 4 | @author K.Townsend (Adafruit Industries) |
Sambo007 | 2:976073ea60df | 5 | @license BSD (see LICENSE.txt) |
Sambo007 | 2:976073ea60df | 6 | |
Sambo007 | 2:976073ea60df | 7 | Ported to mbed by Arve Seljebu - arve0.github.io |
Sambo007 | 2:976073ea60df | 8 | |
Sambo007 | 2:976073ea60df | 9 | Driver for the ADS1015/ADS1115 ADC |
Sambo007 | 2:976073ea60df | 10 | |
Sambo007 | 2:976073ea60df | 11 | This is a library for the Adafruit MPL115A2 breakout |
Sambo007 | 2:976073ea60df | 12 | ----> https://www.adafruit.com/products/1083 |
Sambo007 | 2:976073ea60df | 13 | |
Sambo007 | 2:976073ea60df | 14 | Adafruit invests time and resources providing this open source code, |
Sambo007 | 2:976073ea60df | 15 | please support Adafruit and open-source hardware by purchasing |
Sambo007 | 2:976073ea60df | 16 | products from Adafruit! |
Sambo007 | 2:976073ea60df | 17 | |
Sambo007 | 2:976073ea60df | 18 | @section HISTORY |
Sambo007 | 2:976073ea60df | 19 | |
Sambo007 | 2:976073ea60df | 20 | v1.0 - First release |
Sambo007 | 2:976073ea60df | 21 | v1.1 - Added ADS1115 support - W. Earl |
Sambo007 | 2:976073ea60df | 22 | v1.1.1 - Ported to mbed - Arve Seljebu |
Sambo007 | 2:976073ea60df | 23 | v1.2 - Fixed error in readADC_SingleEnded() sign bit - Sam Berjawi |
Sambo007 | 2:976073ea60df | 24 | v1.3 - Added ADS1115_REG_CONFIG_DR & m_dataRate - Sam W Berjawi Jul 21. 2014 |
Sambo007 | 2:976073ea60df | 25 | - Corrected m_conversionDelay |
Sambo007 | 2:976073ea60df | 26 | - Now there is readADC() that returns counts and readADC_V() that returns voltage |
Sambo007 | 2:976073ea60df | 27 | */ |
Sambo007 | 2:976073ea60df | 28 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 29 | |
Sambo007 | 2:976073ea60df | 30 | #include "ADS1x15.h" |
Sambo007 | 2:976073ea60df | 31 | |
Sambo007 | 2:976073ea60df | 32 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 33 | /*! |
Sambo007 | 2:976073ea60df | 34 | @brief Writes 16-bits to the specified destination register |
Sambo007 | 2:976073ea60df | 35 | */ |
Sambo007 | 2:976073ea60df | 36 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 37 | void ADS1015::writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value) { |
Sambo007 | 2:976073ea60df | 38 | char cmd[3]; |
Sambo007 | 2:976073ea60df | 39 | cmd[0] = (char)reg; |
Sambo007 | 2:976073ea60df | 40 | cmd[1] = (char)(value>>8); |
Sambo007 | 2:976073ea60df | 41 | cmd[2] = (char)(value & 0xFF); |
Sambo007 | 2:976073ea60df | 42 | m_i2c->write(i2cAddress, cmd, 3); |
Sambo007 | 2:976073ea60df | 43 | } |
Sambo007 | 2:976073ea60df | 44 | |
Sambo007 | 2:976073ea60df | 45 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 46 | /*! |
Sambo007 | 2:976073ea60df | 47 | @brief Reads 16-bits from the specified register |
Sambo007 | 2:976073ea60df | 48 | */ |
Sambo007 | 2:976073ea60df | 49 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 50 | uint16_t ADS1015::readRegister(uint8_t i2cAddress, uint8_t reg) { |
Sambo007 | 2:976073ea60df | 51 | char data[2]; |
Sambo007 | 2:976073ea60df | 52 | data[0] = reg; // temporary use this to send address to conversion register |
Sambo007 | 2:976073ea60df | 53 | m_i2c->write(i2cAddress, data, 1); |
Sambo007 | 2:976073ea60df | 54 | m_i2c->read(i2cAddress, data, 2); |
Sambo007 | 2:976073ea60df | 55 | return (data[0] << 8 | data [1]); |
Sambo007 | 2:976073ea60df | 56 | } |
Sambo007 | 2:976073ea60df | 57 | |
Sambo007 | 2:976073ea60df | 58 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 59 | /*! |
Sambo007 | 2:976073ea60df | 60 | @brief Instantiates a new ADS1015 class w/appropriate properties |
Sambo007 | 2:976073ea60df | 61 | */ |
Sambo007 | 2:976073ea60df | 62 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 63 | ADS1015::ADS1015(I2C* i2c, uint8_t i2cAddress) |
Sambo007 | 2:976073ea60df | 64 | { |
Sambo007 | 2:976073ea60df | 65 | // shift 7 bit address 1 left: read expects 8 bit address, see I2C.h |
Sambo007 | 2:976073ea60df | 66 | m_i2cAddress = i2cAddress << 1; |
Sambo007 | 2:976073ea60df | 67 | m_bitShift = 4; |
Sambo007 | 2:976073ea60df | 68 | m_i2c = i2c; |
Sambo007 | 2:976073ea60df | 69 | } |
Sambo007 | 2:976073ea60df | 70 | |
Sambo007 | 2:976073ea60df | 71 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 72 | /*! |
Sambo007 | 2:976073ea60df | 73 | @brief Instantiates a new ADS1115 class w/appropriate properties |
Sambo007 | 2:976073ea60df | 74 | */ |
Sambo007 | 2:976073ea60df | 75 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 76 | ADS1115::ADS1115(I2C* i2c, uint8_t i2cAddress) |
Sambo007 | 2:976073ea60df | 77 | { |
Sambo007 | 2:976073ea60df | 78 | // shift 7 bit address 1 left: read expects 8 bit address, see mbed's I2C.h |
Sambo007 | 2:976073ea60df | 79 | m_i2cAddress = i2cAddress << 1; |
Sambo007 | 2:976073ea60df | 80 | m_bitShift = 0; |
Sambo007 | 2:976073ea60df | 81 | m_i2c = i2c; |
Sambo007 | 2:976073ea60df | 82 | } |
Sambo007 | 2:976073ea60df | 83 | |
Sambo007 | 2:976073ea60df | 84 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 85 | /*! |
Sambo007 | 2:976073ea60df | 86 | @brief Reads the A2D conversion result, measuring depending on chan parameter either |
Sambo007 | 2:976073ea60df | 87 | the single chan voltage or the differential voltage between the P (AIN_i) and N (AIN_j) inputs. |
Sambo007 | 2:976073ea60df | 88 | Generates a signed value since the difference can be either positive or negative. |
Sambo007 | 2:976073ea60df | 89 | |
Sambo007 | 2:976073ea60df | 90 | The +/-6.144V and +/-4.096V settings express the full-scale range of the ADC scaling. |
Sambo007 | 2:976073ea60df | 91 | In no event should more than VDD + 0.3V be applied to this device |
Sambo007 | 2:976073ea60df | 92 | |
Sambo007 | 2:976073ea60df | 93 | @return Count |
Sambo007 | 2:976073ea60df | 94 | */ |
Sambo007 | 2:976073ea60df | 95 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 96 | int16_t ADS1015::readADC(chan_t chan, adsVR_t voltageRange, adsDR_t dataRate) |
Sambo007 | 2:976073ea60df | 97 | { |
Sambo007 | 2:976073ea60df | 98 | // Prepare Config Register |
Sambo007 | 2:976073ea60df | 99 | uint16_t config = ADS1015_REG_CONFIG_OS_SINGLE | // Begin a single conversion (when in power-down mode) |
Sambo007 | 2:976073ea60df | 100 | ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
Sambo007 | 2:976073ea60df | 101 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
Sambo007 | 2:976073ea60df | 102 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
Sambo007 | 2:976073ea60df | 103 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
Sambo007 | 2:976073ea60df | 104 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
Sambo007 | 2:976073ea60df | 105 | |
Sambo007 | 2:976073ea60df | 106 | // Set channel(s) |
Sambo007 | 2:976073ea60df | 107 | config |= chan; |
Sambo007 | 2:976073ea60df | 108 | |
Sambo007 | 2:976073ea60df | 109 | // Set PGA/voltage range |
Sambo007 | 2:976073ea60df | 110 | config |= voltageRange; |
Sambo007 | 2:976073ea60df | 111 | |
Sambo007 | 2:976073ea60df | 112 | // Set Data rate |
Sambo007 | 2:976073ea60df | 113 | config |= dataRate; |
Sambo007 | 2:976073ea60df | 114 | |
Sambo007 | 2:976073ea60df | 115 | // Write config register to the ADC |
Sambo007 | 2:976073ea60df | 116 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
Sambo007 | 2:976073ea60df | 117 | |
Sambo007 | 2:976073ea60df | 118 | // TODO: Modify to alternatively use the RDY (EOC) pin instead of waiting. Maybe also use interrupt and continuous mode |
Sambo007 | 2:976073ea60df | 119 | |
Sambo007 | 2:976073ea60df | 120 | if (m_bitShift == 4) // ADS1015 |
Sambo007 | 2:976073ea60df | 121 | switch (dataRate) { |
Sambo007 | 2:976073ea60df | 122 | case ADS1015_DR_128SPS: |
Sambo007 | 2:976073ea60df | 123 | m_conversionDelay = 1.0 / 128; |
Sambo007 | 2:976073ea60df | 124 | break; |
Sambo007 | 2:976073ea60df | 125 | case ADS1015_DR_250SPS: |
Sambo007 | 2:976073ea60df | 126 | m_conversionDelay = 1.0 / 250; |
Sambo007 | 2:976073ea60df | 127 | break; |
Sambo007 | 2:976073ea60df | 128 | case ADS1015_DR_490SPS: |
Sambo007 | 2:976073ea60df | 129 | m_conversionDelay = 1.0 / 490; |
Sambo007 | 2:976073ea60df | 130 | break; |
Sambo007 | 2:976073ea60df | 131 | case ADS1015_DR_920SPS: |
Sambo007 | 2:976073ea60df | 132 | m_conversionDelay = 1.0 / 920; |
Sambo007 | 2:976073ea60df | 133 | break; |
Sambo007 | 2:976073ea60df | 134 | case ADS1015_DR_1600SPS: |
Sambo007 | 2:976073ea60df | 135 | m_conversionDelay = 1.0 / 1600; |
Sambo007 | 2:976073ea60df | 136 | break; |
Sambo007 | 2:976073ea60df | 137 | case ADS1015_DR_2400SPS: |
Sambo007 | 2:976073ea60df | 138 | m_conversionDelay = 1.0 / 2400; |
Sambo007 | 2:976073ea60df | 139 | break; |
Sambo007 | 2:976073ea60df | 140 | case ADS1015_DR_3300SPS: |
Sambo007 | 2:976073ea60df | 141 | m_conversionDelay = 1.0 / 3300; |
Sambo007 | 2:976073ea60df | 142 | break; |
Sambo007 | 2:976073ea60df | 143 | } |
Sambo007 | 2:976073ea60df | 144 | else // ADS1115 |
Sambo007 | 2:976073ea60df | 145 | switch (dataRate) { |
Sambo007 | 2:976073ea60df | 146 | case ADS1115_DR_8SPS: |
Sambo007 | 2:976073ea60df | 147 | m_conversionDelay = 1.0 / 8; |
Sambo007 | 2:976073ea60df | 148 | break; |
Sambo007 | 2:976073ea60df | 149 | case ADS1115_DR_16SPS: |
Sambo007 | 2:976073ea60df | 150 | m_conversionDelay = 1.0 / 16; |
Sambo007 | 2:976073ea60df | 151 | break; |
Sambo007 | 2:976073ea60df | 152 | case ADS1115_DR_32SPS: |
Sambo007 | 2:976073ea60df | 153 | m_conversionDelay = 1.0 / 32; |
Sambo007 | 2:976073ea60df | 154 | break; |
Sambo007 | 2:976073ea60df | 155 | case ADS1115_DR_64SPS: |
Sambo007 | 2:976073ea60df | 156 | m_conversionDelay = 1.0 / 64; |
Sambo007 | 2:976073ea60df | 157 | break; |
Sambo007 | 2:976073ea60df | 158 | case ADS1115_DR_128SPS: |
Sambo007 | 2:976073ea60df | 159 | m_conversionDelay = 1.0 / 128; |
Sambo007 | 2:976073ea60df | 160 | break; |
Sambo007 | 2:976073ea60df | 161 | case ADS1115_DR_250SPS: |
Sambo007 | 2:976073ea60df | 162 | m_conversionDelay = 1.0 / 250; |
Sambo007 | 2:976073ea60df | 163 | break; |
Sambo007 | 2:976073ea60df | 164 | case ADS1115_DR_475SPS: |
Sambo007 | 2:976073ea60df | 165 | m_conversionDelay = 1.0 / 475; |
Sambo007 | 2:976073ea60df | 166 | break; |
Sambo007 | 2:976073ea60df | 167 | case ADS1115_DR_860SPS: |
Sambo007 | 2:976073ea60df | 168 | m_conversionDelay = 1.0 / 860; |
Sambo007 | 2:976073ea60df | 169 | break; |
Sambo007 | 2:976073ea60df | 170 | } |
Sambo007 | 2:976073ea60df | 171 | |
Sambo007 | 2:976073ea60df | 172 | // Wait for the conversion to complete |
Sambo007 | 2:976073ea60df | 173 | wait(m_conversionDelay); |
Sambo007 | 2:976073ea60df | 174 | |
Sambo007 | 2:976073ea60df | 175 | // Read the conversion results |
Sambo007 | 2:976073ea60df | 176 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
Sambo007 | 2:976073ea60df | 177 | if (m_bitShift == 0) |
Sambo007 | 2:976073ea60df | 178 | { |
Sambo007 | 2:976073ea60df | 179 | return (int16_t)res; |
Sambo007 | 2:976073ea60df | 180 | } |
Sambo007 | 2:976073ea60df | 181 | else |
Sambo007 | 2:976073ea60df | 182 | { |
Sambo007 | 2:976073ea60df | 183 | // Shift 12-bit results right 4 bits for the ADS1015, |
Sambo007 | 2:976073ea60df | 184 | // making sure we keep the sign bit intact |
Sambo007 | 2:976073ea60df | 185 | if (res > 0x07FF) |
Sambo007 | 2:976073ea60df | 186 | { |
Sambo007 | 2:976073ea60df | 187 | // negative number - extend the sign to 16th bit |
Sambo007 | 2:976073ea60df | 188 | res |= 0xF000; |
Sambo007 | 2:976073ea60df | 189 | } |
Sambo007 | 2:976073ea60df | 190 | return (int16_t)res; |
Sambo007 | 2:976073ea60df | 191 | } |
Sambo007 | 2:976073ea60df | 192 | } |
Sambo007 | 2:976073ea60df | 193 | |
Sambo007 | 2:976073ea60df | 194 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 195 | /*! |
Sambo007 | 2:976073ea60df | 196 | @brief Reads the A2D conversion result, measuring depending on chan parameter either |
Sambo007 | 2:976073ea60df | 197 | the single chan voltage or the differential voltage between the P (AIN_i) and N (AIN_j) inputs. |
Sambo007 | 2:976073ea60df | 198 | Generates a signed value since the difference can be either positive or negative. |
Sambo007 | 2:976073ea60df | 199 | |
Sambo007 | 2:976073ea60df | 200 | The +/-6.144V and +/-4.096V settings express the full-scale range of the ADC scaling. |
Sambo007 | 2:976073ea60df | 201 | In no event should more than VDD + 0.3V be applied to this device |
Sambo007 | 2:976073ea60df | 202 | |
Sambo007 | 2:976073ea60df | 203 | @return Voltage |
Sambo007 | 2:976073ea60df | 204 | */ |
Sambo007 | 2:976073ea60df | 205 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 206 | float ADS1015::readADC_V(chan_t chan, adsVR_t voltageRange, adsDR_t dataRate) |
Sambo007 | 2:976073ea60df | 207 | { |
Sambo007 | 2:976073ea60df | 208 | float bit_V = 0.; |
Sambo007 | 2:976073ea60df | 209 | |
Sambo007 | 2:976073ea60df | 210 | // ADS1015 ADS1115 |
Sambo007 | 2:976073ea60df | 211 | // +/-6.144V range = Gain 2/3 1-bit = 3mV 0.1875mV |
Sambo007 | 2:976073ea60df | 212 | // +/-4.096V range = Gain 1 1-bit = 2mV 0.125mV |
Sambo007 | 2:976073ea60df | 213 | // +/-2.048V range = Gain 2 1-bit = 1mV 0.0625mV |
Sambo007 | 2:976073ea60df | 214 | // +/-1.024V range = Gain 4 1-bit = 0.5mV 0.03125mV |
Sambo007 | 2:976073ea60df | 215 | // +/-0.512V range = Gain 8 1-bit = 0.25mV 0.015625mV |
Sambo007 | 2:976073ea60df | 216 | // +/-0.256V range = Gain 16 1-bit = 0.125mV 0.0078125mV |
Sambo007 | 2:976073ea60df | 217 | |
Sambo007 | 2:976073ea60df | 218 | switch (voltageRange) { |
Sambo007 | 2:976073ea60df | 219 | case VR_p_m_6_144V: |
Sambo007 | 2:976073ea60df | 220 | bit_V = 3.0e-3f * powf(2, -4 + m_bitShift); |
Sambo007 | 2:976073ea60df | 221 | break; |
Sambo007 | 2:976073ea60df | 222 | case VR_p_m_4_096V: |
Sambo007 | 2:976073ea60df | 223 | bit_V = 2.0e-3f * powf(2, -4 + m_bitShift); |
Sambo007 | 2:976073ea60df | 224 | break; |
Sambo007 | 2:976073ea60df | 225 | case VR_p_m_2_048V: |
Sambo007 | 2:976073ea60df | 226 | bit_V = 1.0e-3f * powf(2, -4 + m_bitShift); |
Sambo007 | 2:976073ea60df | 227 | break; |
Sambo007 | 2:976073ea60df | 228 | case VR_p_m_1_024V: |
Sambo007 | 2:976073ea60df | 229 | bit_V = 0.5e-3f * powf(2, -4 + m_bitShift); |
Sambo007 | 2:976073ea60df | 230 | break; |
Sambo007 | 2:976073ea60df | 231 | case VR_p_m_0_512V: |
Sambo007 | 2:976073ea60df | 232 | bit_V = 0.25e-3f * powf(2, -4 + m_bitShift); |
Sambo007 | 2:976073ea60df | 233 | break; |
Sambo007 | 2:976073ea60df | 234 | case VR_p_m_0_256V: |
Sambo007 | 2:976073ea60df | 235 | bit_V = 0.125e-3f * powf(2, -4 + m_bitShift); |
Sambo007 | 2:976073ea60df | 236 | break; |
Sambo007 | 2:976073ea60df | 237 | } |
Sambo007 | 2:976073ea60df | 238 | |
Sambo007 | 2:976073ea60df | 239 | return (readADC(chan, voltageRange, dataRate) * bit_V); |
Sambo007 | 2:976073ea60df | 240 | } |
Sambo007 | 2:976073ea60df | 241 | |
Sambo007 | 2:976073ea60df | 242 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 243 | /*! |
Sambo007 | 2:976073ea60df | 244 | @brief Sets up the comparator to operate in basic mode, causing the |
Sambo007 | 2:976073ea60df | 245 | ALERT/RDY pin to assert (go from high to low) when the ADC |
Sambo007 | 2:976073ea60df | 246 | value exceeds the specified threshold. |
Sambo007 | 2:976073ea60df | 247 | |
Sambo007 | 2:976073ea60df | 248 | This will also set the ADC in continuous conversion mode. |
Sambo007 | 2:976073ea60df | 249 | */ |
Sambo007 | 2:976073ea60df | 250 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 251 | void ADS1015::startComparator_SingleEnded(chan_t chan, adsVR_t voltageRange, adsDR_t dataRate, int16_t threshold) |
Sambo007 | 2:976073ea60df | 252 | { |
Sambo007 | 2:976073ea60df | 253 | // Prepare Config Register |
Sambo007 | 2:976073ea60df | 254 | uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match |
Sambo007 | 2:976073ea60df | 255 | ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode |
Sambo007 | 2:976073ea60df | 256 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
Sambo007 | 2:976073ea60df | 257 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
Sambo007 | 2:976073ea60df | 258 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
Sambo007 | 2:976073ea60df | 259 | |
Sambo007 | 2:976073ea60df | 260 | // Set channel(s) |
Sambo007 | 2:976073ea60df | 261 | config |= chan; |
Sambo007 | 2:976073ea60df | 262 | |
Sambo007 | 2:976073ea60df | 263 | // Set PGA/voltage range |
Sambo007 | 2:976073ea60df | 264 | config |= voltageRange; |
Sambo007 | 2:976073ea60df | 265 | |
Sambo007 | 2:976073ea60df | 266 | // Set Data rate |
Sambo007 | 2:976073ea60df | 267 | config |= dataRate; |
Sambo007 | 2:976073ea60df | 268 | |
Sambo007 | 2:976073ea60df | 269 | // Set the high threshold register |
Sambo007 | 2:976073ea60df | 270 | // Shift 12-bit results left 4 bits for the ADS1015 |
Sambo007 | 2:976073ea60df | 271 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift); |
Sambo007 | 2:976073ea60df | 272 | |
Sambo007 | 2:976073ea60df | 273 | // Write config register to the ADC |
Sambo007 | 2:976073ea60df | 274 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
Sambo007 | 2:976073ea60df | 275 | } |
Sambo007 | 2:976073ea60df | 276 | |
Sambo007 | 2:976073ea60df | 277 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 278 | /*! |
Sambo007 | 2:976073ea60df | 279 | @brief In order to clear the comparator, we need to read the |
Sambo007 | 2:976073ea60df | 280 | conversion results. This function reads the last conversion |
Sambo007 | 2:976073ea60df | 281 | results without changing the config value. |
Sambo007 | 2:976073ea60df | 282 | */ |
Sambo007 | 2:976073ea60df | 283 | /**************************************************************************/ |
Sambo007 | 2:976073ea60df | 284 | int16_t ADS1015::getLastConversionResults() |
Sambo007 | 2:976073ea60df | 285 | { |
Sambo007 | 2:976073ea60df | 286 | // Wait for the conversion to complete |
Sambo007 | 2:976073ea60df | 287 | wait_ms(m_conversionDelay); |
Sambo007 | 2:976073ea60df | 288 | |
Sambo007 | 2:976073ea60df | 289 | // Read the conversion results |
Sambo007 | 2:976073ea60df | 290 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
Sambo007 | 2:976073ea60df | 291 | if (m_bitShift == 0) |
Sambo007 | 2:976073ea60df | 292 | { |
Sambo007 | 2:976073ea60df | 293 | return (int16_t)res; |
Sambo007 | 2:976073ea60df | 294 | } |
Sambo007 | 2:976073ea60df | 295 | else |
Sambo007 | 2:976073ea60df | 296 | { |
Sambo007 | 2:976073ea60df | 297 | // Shift 12-bit results right 4 bits for the ADS1015, |
Sambo007 | 2:976073ea60df | 298 | // making sure we keep the sign bit intact |
Sambo007 | 2:976073ea60df | 299 | if (res > 0x07FF) |
Sambo007 | 2:976073ea60df | 300 | { |
Sambo007 | 2:976073ea60df | 301 | // negative number - extend the sign to 16th bit |
Sambo007 | 2:976073ea60df | 302 | res |= 0xF000; |
Sambo007 | 2:976073ea60df | 303 | } |
Sambo007 | 2:976073ea60df | 304 | return (int16_t)res; |
Sambo007 | 2:976073ea60df | 305 | } |
Sambo007 | 2:976073ea60df | 306 | } |