Berhasil
Dependents: ADS1115_IR Test_all
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 char cmd[3]; 00036 cmd[0] = (char)reg; 00037 cmd[1] = (char)(value>>8); 00038 cmd[2] = (char)(value & 0xFF); 00039 m_i2c->write(i2cAddress, cmd, 3); 00040 } 00041 00042 /**************************************************************************/ 00043 /*! 00044 @brief Reads 16-bits from the specified register 00045 */ 00046 /**************************************************************************/ 00047 uint16_t Adafruit_ADS1015::readRegister(uint8_t i2cAddress, uint8_t reg) 00048 { 00049 char data[2]; 00050 data[0] = reg; // temporary use this to send address to conversion register 00051 m_i2c->write(i2cAddress, data, 1); 00052 m_i2c->read(i2cAddress, data, 2); 00053 return (data[0] << 8 | data [1]); 00054 } 00055 00056 /**************************************************************************/ 00057 /*! 00058 @brief Instantiates a new ADS1015 class w/appropriate properties 00059 */ 00060 /**************************************************************************/ 00061 Adafruit_ADS1015::Adafruit_ADS1015(I2C* i2c, uint8_t i2cAddress) 00062 { 00063 // shift 7 bit address 1 left: read expects 8 bit address, see I2C.h 00064 m_i2cAddress = i2cAddress << 1; 00065 m_conversionDelay = ADS1015_CONVERSIONDELAY; 00066 m_bitShift = 4; 00067 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ 00068 // m_gain = GAIN_TWO; /* +/- 2.048V range (limited to VDD +0.3V max!) */ 00069 m_i2c = i2c; 00070 } 00071 00072 /**************************************************************************/ 00073 /*! 00074 @brief Instantiates a new ADS1115 class w/appropriate properties 00075 */ 00076 /**************************************************************************/ 00077 Adafruit_ADS1115::Adafruit_ADS1115(I2C* i2c, uint8_t i2cAddress) 00078 { 00079 // shift 7 bit address 1 left: read expects 8 bit address, see mbed's I2C.h 00080 m_i2cAddress = i2cAddress << 1; 00081 m_conversionDelay = ADS1115_CONVERSIONDELAY; 00082 m_bitShift = 0; 00083 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ 00084 // m_gain = GAIN_TWO; /* +/- 2.048V range (limited to VDD +0.3V max!) */ 00085 m_i2c = i2c; 00086 } 00087 00088 /**************************************************************************/ 00089 /*! 00090 @brief Sets the gain and input voltage range 00091 */ 00092 /**************************************************************************/ 00093 void Adafruit_ADS1015::setGain(adsGain_t gain) 00094 { 00095 m_gain = gain; 00096 } 00097 00098 /**************************************************************************/ 00099 /*! 00100 @brief Gets a gain and input voltage range 00101 */ 00102 /**************************************************************************/ 00103 adsGain_t Adafruit_ADS1015::getGain() 00104 { 00105 return m_gain; 00106 } 00107 00108 /**************************************************************************/ 00109 /*! 00110 @brief Gets a single-ended ADC reading from the specified channel 00111 */ 00112 /**************************************************************************/ 00113 uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) 00114 { 00115 if (channel > 3) { 00116 return 0; 00117 } 00118 00119 // Start with default values 00120 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) 00121 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 00122 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00123 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00124 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00125 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) 00126 00127 // Set PGA/voltage range 00128 config |= m_gain; 00129 00130 // Set single-ended input channel 00131 switch (channel) { 00132 case (0): 00133 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; 00134 break; 00135 case (1): 00136 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; 00137 break; 00138 case (2): 00139 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; 00140 break; 00141 case (3): 00142 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; 00143 break; 00144 } 00145 00146 // Set 'start single-conversion' bit 00147 config |= ADS1015_REG_CONFIG_OS_SINGLE; 00148 00149 // Write config register to the ADC 00150 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00151 00152 // Wait for the conversion to complete 00153 wait_ms(m_conversionDelay); 00154 00155 // Read the conversion results 00156 // Shift 12-bit results right 4 bits for the ADS1015 00157 return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00158 } 00159 00160 /**************************************************************************/ 00161 /*! 00162 @brief Reads the conversion results, measuring the voltage 00163 difference between the P (AIN0) and N (AIN1) input. Generates 00164 a signed value since the difference can be either 00165 positive or negative. 00166 */ 00167 /**************************************************************************/ 00168 int16_t Adafruit_ADS1015::readADC_Differential_0_1() 00169 { 00170 // Start with default values 00171 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) 00172 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 00173 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00174 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00175 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00176 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) 00177 00178 // Set PGA/voltage range 00179 config |= m_gain; 00180 00181 // Set channels 00182 config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N 00183 00184 // Set 'start single-conversion' bit 00185 config |= ADS1015_REG_CONFIG_OS_SINGLE; 00186 00187 // Write config register to the ADC 00188 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00189 00190 // Wait for the conversion to complete 00191 wait_ms(m_conversionDelay); 00192 00193 // Read the conversion results 00194 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00195 if (m_bitShift == 0) { 00196 return (int16_t)res; 00197 } else { 00198 // Shift 12-bit results right 4 bits for the ADS1015, 00199 // making sure we keep the sign bit intact 00200 if (res > 0x07FF) { 00201 // negative number - extend the sign to 16th bit 00202 res |= 0xF000; 00203 } 00204 return (int16_t)res; 00205 } 00206 } 00207 00208 /**************************************************************************/ 00209 /*! 00210 @brief Reads the conversion results, measuring the voltage 00211 difference between the P (AIN2) and N (AIN3) input. Generates 00212 a signed value since the difference can be either 00213 positive or negative. 00214 */ 00215 /**************************************************************************/ 00216 int16_t Adafruit_ADS1015::readADC_Differential_2_3() 00217 { 00218 // Start with default values 00219 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) 00220 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 00221 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00222 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00223 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00224 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) 00225 00226 // Set PGA/voltage range 00227 config |= m_gain; 00228 00229 // Set channels 00230 config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N 00231 00232 // Set 'start single-conversion' bit 00233 config |= ADS1015_REG_CONFIG_OS_SINGLE; 00234 00235 // Write config register to the ADC 00236 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00237 00238 // Wait for the conversion to complete 00239 wait_ms(m_conversionDelay); 00240 00241 // Read the conversion results 00242 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00243 if (m_bitShift == 0) { 00244 return (int16_t)res; 00245 } else { 00246 // Shift 12-bit results right 4 bits for the ADS1015, 00247 // making sure we keep the sign bit intact 00248 if (res > 0x07FF) { 00249 // negative number - extend the sign to 16th bit 00250 res |= 0xF000; 00251 } 00252 return (int16_t)res; 00253 } 00254 } 00255 00256 /**************************************************************************/ 00257 /*! 00258 @brief Sets up the comparator to operate in basic mode, causing the 00259 ALERT/RDY pin to assert (go from high to low) when the ADC 00260 value exceeds the specified threshold. 00261 00262 This will also set the ADC in continuous conversion mode. 00263 */ 00264 /**************************************************************************/ 00265 void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold) 00266 { 00267 // Start with default values 00268 uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match 00269 ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode 00270 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 00271 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 00272 ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) 00273 ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode 00274 ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode 00275 00276 // Set PGA/voltage range 00277 config |= m_gain; 00278 00279 // Set single-ended input channel 00280 switch (channel) { 00281 case (0): 00282 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; 00283 break; 00284 case (1): 00285 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; 00286 break; 00287 case (2): 00288 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; 00289 break; 00290 case (3): 00291 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; 00292 break; 00293 } 00294 00295 // Set the high threshold register 00296 // Shift 12-bit results left 4 bits for the ADS1015 00297 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift); 00298 00299 // Write config register to the ADC 00300 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); 00301 } 00302 00303 /**************************************************************************/ 00304 /*! 00305 @brief In order to clear the comparator, we need to read the 00306 conversion results. This function reads the last conversion 00307 results without changing the config value. 00308 */ 00309 /**************************************************************************/ 00310 int16_t Adafruit_ADS1015::getLastConversionResults() 00311 { 00312 // Wait for the conversion to complete 00313 wait_ms(m_conversionDelay); 00314 00315 // Read the conversion results 00316 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; 00317 if (m_bitShift == 0) { 00318 return (int16_t)res; 00319 } else { 00320 // Shift 12-bit results right 4 bits for the ADS1015, 00321 // making sure we keep the sign bit intact 00322 if (res > 0x07FF) { 00323 // negative number - extend the sign to 16th bit 00324 res |= 0xF000; 00325 } 00326 return (int16_t)res; 00327 } 00328 }
Generated on Wed Aug 17 2022 15:02:47 by
1.7.2