Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ADS1015 by
Adafruit_ADS1015.cpp
00001 /**************************************************************************/ 00002 /*! 00003 @file Adafruit_ADS1015.cpp 00004 @author K.Townsend (Adafruit Industries) 00005 @license BSD (see LICENSE.txt) 00006 00007 Ported to mbed by Arve Seljebu - arve0.github.io 00008 00009 Driver for the ADS1015/ADS1115 ADC 00010 00011 This is a library for the Adafruit MPL115A2 breakout 00012 ----> https://www.adafruit.com/products/1083 00013 00014 Adafruit invests time and resources providing this open source code, 00015 please support Adafruit and open-source hardware by purchasing 00016 products from Adafruit! 00017 00018 @section HISTORY 00019 00020 v1.0 - First release 00021 v1.1.1 - Ported to mbed 00022 */ 00023 /**************************************************************************/ 00024 00025 #include <mbed.h> 00026 #include "Adafruit_ADS1015.h " 00027 00028 /**************************************************************************/ 00029 /*! 00030 @brief Writes 16-bits to the specified destination register 00031 */ 00032 /**************************************************************************/ 00033 void Adafruit_ADS1015::writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value) 00034 { 00035 //printf("ADC-wr1\r\n"); 00036 char cmd[3]; 00037 cmd[0] = (char)reg; 00038 //printf("ADC-wr cmd0=%i\r\n", cmd[0]); 00039 //printf("ADC-wr2\r\n"); 00040 cmd[1] = (char)(value>>8); 00041 //printf("ADC-wr cmd1=%i\r\n", cmd[1]); 00042 //printf("ADC-wr3\r\n"); 00043 cmd[2] = (char)(value & 0xFF); 00044 //printf("ADC-wr cmd2=%i\r\n", cmd[2]); 00045 //printf("ADC-wr4\r\n"); 00046 //printf("i2c-Write return = %i\r\n", m_i2c->write(i2cAddress, cmd, 3)); 00047 m_i2c->write(i2cAddress, cmd, 3); 00048 //printf("ADC-wr5\r\n"); 00049 } 00050 00051 /**************************************************************************/ 00052 /*! 00053 @brief Reads 16-bits from the specified register 00054 */ 00055 /**************************************************************************/ 00056 uint16_t Adafruit_ADS1015::readRegister(uint8_t i2cAddress, uint8_t reg) 00057 { 00058 //printf("ADC-RR1\r\n"); 00059 char data[2]; 00060 //printf("ADC-RR2\r\n"); 00061 data[0] = reg; // temporary use this to send address to conversion register 00062 //printf("ADC-RR3\r\n"); 00063 m_i2c->write(i2cAddress, data, 1); 00064 //printf("ADC-RR4\r\n"); 00065 m_i2c->read(i2cAddress, data, 2); 00066 //printf("ADC-RR5\r\n"); 00067 return (data[0] << 8 | data [1]); 00068 } 00069 00070 /**************************************************************************/ 00071 /*! 00072 @brief Instantiates a new ADS1015 class w/appropriate properties 00073 */ 00074 /**************************************************************************/ 00075 Adafruit_ADS1015::Adafruit_ADS1015(I2C* i2c, uint8_t i2cAddress) 00076 { 00077 // shift 7 bit address 1 left: read expects 8 bit address, see I2C.h 00078 m_i2cAddress = i2cAddress << 1; 00079 m_conversionDelay = ADS1015_CONVERSIONDELAY; 00080 m_bitShift = 4; 00081 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ 00082 m_i2c = i2c; 00083 } 00084 00085 /**************************************************************************/ 00086 /*! 00087 @brief Instantiates a new ADS1115 class w/appropriate properties 00088 */ 00089 /**************************************************************************/ 00090 Adafruit_ADS1115::Adafruit_ADS1115(I2C* i2c, uint8_t i2cAddress) 00091 { 00092 // shift 7 bit address 1 left: read expects 8 bit address, see mbed's I2C.h 00093 m_i2cAddress = i2cAddress << 1; 00094 //printf("12c-address=%i\r\n", m_i2cAddress); 00095 m_conversionDelay = ADS1115_CONVERSIONDELAY; 00096 m_bitShift = 0; 00097 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ 00098 m_i2c = i2c; 00099 } 00100 00101 /**************************************************************************/ 00102 /*! 00103 @brief Sets the gain and input voltage range 00104 */ 00105 /**************************************************************************/ 00106 void Adafruit_ADS1015::setGain(adsGain_t gain) 00107 { 00108 m_gain = gain; 00109 } 00110 00111 /**************************************************************************/ 00112 /*! 00113 @brief Gets a gain and input voltage range 00114 */ 00115 /**************************************************************************/ 00116 adsGain_t Adafruit_ADS1015::getGain() 00117 { 00118 return m_gain; 00119 } 00120 00121 /**************************************************************************/ 00122 /*! 00123 @brief Gets a single-ended ADC reading from the specified channel 00124 */ 00125 /**************************************************************************/ 00126 uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) 00127 { 00128 //printf("ADC1\r\n"); 00129 //printf("channel = %i\r\n", channel); 00130 if (channel > 3) { 00131 //printf("ADC2\r\n"); 00132 return 0; 00133 } 00134 //printf("ADC3\r\n"); 00135 // Start with default values 00136 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) 00137 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 00138 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00139 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00140 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00141 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) 00142 00143 // Set PGA/voltage range 00144 //printf("ADC4\r\n"); 00145 config |= m_gain; 00146 //printf("ADC5\r\n"); 00147 // Set single-ended input channel 00148 switch (channel) { 00149 case (0): 00150 //printf("ADC6\r\n"); 00151 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; 00152 //printf("ADC7\r\n"); 00153 break; 00154 case (1): 00155 //printf("ADC8\r\n"); 00156 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; 00157 //printf("ADC9\r\n"); 00158 break; 00159 case (2): 00160 //printf("ADC10\r\n"); 00161 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; 00162 //printf("ADC11\r\n"); 00163 break; 00164 case (3): 00165 //printf("ADC12\r\n"); 00166 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; 00167 //printf("ADC13\r\n"); 00168 break; 00169 } 00170 00171 // Set 'start single-conversion' bit 00172 //printf("ADC14\r\n"); 00173 config |= ADS1015_REG_CONFIG_OS_SINGLE; 00174 00175 // Write config register to the ADC 00176 //printf("ADC15\r\n"); 00177 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00178 00179 // Wait for the conversion to complete 00180 //printf("ADC16\r\n"); 00181 wait_ms(m_conversionDelay); 00182 00183 // Read the conversion results 00184 // Shift 12-bit results right 4 bits for the ADS1015 00185 //printf("ADC17\r\n"); 00186 return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00187 } 00188 00189 /**************************************************************************/ 00190 /*! 00191 @brief Reads the conversion results, measuring the voltage 00192 difference between the P (AIN0) and N (AIN1) input. Generates 00193 a signed value since the difference can be either 00194 positive or negative. 00195 */ 00196 /**************************************************************************/ 00197 int16_t Adafruit_ADS1015::readADC_Differential_0_1() 00198 { 00199 // Start with default values 00200 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) 00201 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 00202 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00203 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00204 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00205 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) 00206 00207 // Set PGA/voltage range 00208 config |= m_gain; 00209 00210 // Set channels 00211 config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N 00212 00213 // Set 'start single-conversion' bit 00214 config |= ADS1015_REG_CONFIG_OS_SINGLE; 00215 00216 // Write config register to the ADC 00217 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00218 00219 // Wait for the conversion to complete 00220 wait_ms(m_conversionDelay); 00221 00222 // Read the conversion results 00223 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00224 if (m_bitShift == 0) { 00225 return (int16_t)res; 00226 } else { 00227 // Shift 12-bit results right 4 bits for the ADS1015, 00228 // making sure we keep the sign bit intact 00229 if (res > 0x07FF) { 00230 // negative number - extend the sign to 16th bit 00231 res |= 0xF000; 00232 } 00233 return (int16_t)res; 00234 } 00235 } 00236 00237 /**************************************************************************/ 00238 /*! 00239 @brief Reads the conversion results, measuring the voltage 00240 difference between the P (AIN2) and N (AIN3) input. Generates 00241 a signed value since the difference can be either 00242 positive or negative. 00243 */ 00244 /**************************************************************************/ 00245 int16_t Adafruit_ADS1015::readADC_Differential_2_3() 00246 { 00247 // Start with default values 00248 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) 00249 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 00250 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00251 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00252 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00253 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) 00254 00255 // Set PGA/voltage range 00256 config |= m_gain; 00257 00258 // Set channels 00259 config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N 00260 00261 // Set 'start single-conversion' bit 00262 config |= ADS1015_REG_CONFIG_OS_SINGLE; 00263 00264 // Write config register to the ADC 00265 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00266 00267 // Wait for the conversion to complete 00268 wait_ms(m_conversionDelay); 00269 00270 // Read the conversion results 00271 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00272 if (m_bitShift == 0) { 00273 return (int16_t)res; 00274 } else { 00275 // Shift 12-bit results right 4 bits for the ADS1015, 00276 // making sure we keep the sign bit intact 00277 if (res > 0x07FF) { 00278 // negative number - extend the sign to 16th bit 00279 res |= 0xF000; 00280 } 00281 return (int16_t)res; 00282 } 00283 } 00284 00285 /**************************************************************************/ 00286 /*! 00287 @brief Sets up the comparator to operate in basic mode, causing the 00288 ALERT/RDY pin to assert (go from high to low) when the ADC 00289 value exceeds the specified threshold. 00290 00291 This will also set the ADC in continuous conversion mode. 00292 */ 00293 /**************************************************************************/ 00294 void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold) 00295 { 00296 // Start with default values 00297 uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match 00298 ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode 00299 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00300 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00301 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00302 ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode 00303 ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode 00304 00305 // Set PGA/voltage range 00306 config |= m_gain; 00307 00308 // Set single-ended input channel 00309 switch (channel) { 00310 case (0): 00311 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; 00312 break; 00313 case (1): 00314 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; 00315 break; 00316 case (2): 00317 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; 00318 break; 00319 case (3): 00320 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; 00321 break; 00322 } 00323 00324 // Set the high threshold register 00325 // Shift 12-bit results left 4 bits for the ADS1015 00326 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift); 00327 00328 // Write config register to the ADC 00329 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00330 } 00331 00332 /**************************************************************************/ 00333 /*! 00334 @brief In order to clear the comparator, we need to read the 00335 conversion results. This function reads the last conversion 00336 results without changing the config value. 00337 */ 00338 /**************************************************************************/ 00339 int16_t Adafruit_ADS1015::getLastConversionResults() 00340 { 00341 // Wait for the conversion to complete 00342 wait_ms(m_conversionDelay); 00343 00344 // Read the conversion results 00345 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00346 if (m_bitShift == 0) { 00347 return (int16_t)res; 00348 } else { 00349 // Shift 12-bit results right 4 bits for the ADS1015, 00350 // making sure we keep the sign bit intact 00351 if (res > 0x07FF) { 00352 // negative number - extend the sign to 16th bit 00353 res |= 0xF000; 00354 } 00355 return (int16_t)res; 00356 } 00357 }
Generated on Tue Jul 12 2022 18:47:52 by
1.7.2
