readADC_Differential_0_1()のconfigの値の設定を readADC_Differential_2_3()のconfigの値と同じにしました

Fork of ads1115_test by Gaku Matsumoto

Committer:
PQUTO
Date:
Wed Feb 22 18:22:42 2017 +0000
Revision:
2:68c4863a39cd
Parent:
0:387c3e3b28f1
readADC_Differential_0_1()?config??????readADC_Differential_2_3()?config?????????????

Who changed what in which revision?

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