readADC_Differential_0_1()のconfigの値の設定を readADC_Differential_2_3()のconfigの値と同じにしました
Fork of ads1115_test by
Adafruit_ADS1015.cpp@0:387c3e3b28f1, 2016-09-01 (annotated)
- Committer:
- Gaku0606
- Date:
- Thu Sep 01 13:42:16 2016 +0000
- Revision:
- 0:387c3e3b28f1
- Child:
- 2:68c4863a39cd
ads1115
Who changed what in which revision?
User | Revision | Line number | New 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 |
Gaku0606 | 0:387c3e3b28f1 | 198 | uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) |
Gaku0606 | 0:387c3e3b28f1 | 199 | ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) |
Gaku0606 | 0:387c3e3b28f1 | 200 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
Gaku0606 | 0:387c3e3b28f1 | 201 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
Gaku0606 | 0:387c3e3b28f1 | 202 | ADS1015_REG_CONFIG_DR_1600SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
Gaku0606 | 0:387c3e3b28f1 | 203 | ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) |
Gaku0606 | 0:387c3e3b28f1 | 204 | |
Gaku0606 | 0:387c3e3b28f1 | 205 | // Set PGA/voltage range |
Gaku0606 | 0:387c3e3b28f1 | 206 | config |= m_gain; |
Gaku0606 | 0:387c3e3b28f1 | 207 | |
Gaku0606 | 0:387c3e3b28f1 | 208 | // Set channels |
Gaku0606 | 0:387c3e3b28f1 | 209 | config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N |
Gaku0606 | 0:387c3e3b28f1 | 210 | |
Gaku0606 | 0:387c3e3b28f1 | 211 | // Set 'start single-conversion' bit |
Gaku0606 | 0:387c3e3b28f1 | 212 | config |= ADS1015_REG_CONFIG_OS_SINGLE; |
Gaku0606 | 0:387c3e3b28f1 | 213 | |
Gaku0606 | 0:387c3e3b28f1 | 214 | // Write config register to the ADC |
Gaku0606 | 0:387c3e3b28f1 | 215 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
Gaku0606 | 0:387c3e3b28f1 | 216 | |
Gaku0606 | 0:387c3e3b28f1 | 217 | // Wait for the conversion to complete |
Gaku0606 | 0:387c3e3b28f1 | 218 | wait_ms(m_conversionDelay); |
Gaku0606 | 0:387c3e3b28f1 | 219 | |
Gaku0606 | 0:387c3e3b28f1 | 220 | // Read the conversion results |
Gaku0606 | 0:387c3e3b28f1 | 221 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
Gaku0606 | 0:387c3e3b28f1 | 222 | if (m_bitShift == 0) { |
Gaku0606 | 0:387c3e3b28f1 | 223 | return (int16_t)res; |
Gaku0606 | 0:387c3e3b28f1 | 224 | } else { |
Gaku0606 | 0:387c3e3b28f1 | 225 | // Shift 12-bit results right 4 bits for the ADS1015, |
Gaku0606 | 0:387c3e3b28f1 | 226 | // making sure we keep the sign bit intact |
Gaku0606 | 0:387c3e3b28f1 | 227 | if (res > 0x07FF) { |
Gaku0606 | 0:387c3e3b28f1 | 228 | // negative number - extend the sign to 16th bit |
Gaku0606 | 0:387c3e3b28f1 | 229 | res |= 0xF000; |
Gaku0606 | 0:387c3e3b28f1 | 230 | } |
Gaku0606 | 0:387c3e3b28f1 | 231 | return (int16_t)res; |
Gaku0606 | 0:387c3e3b28f1 | 232 | } |
Gaku0606 | 0:387c3e3b28f1 | 233 | } |
Gaku0606 | 0:387c3e3b28f1 | 234 | |
Gaku0606 | 0:387c3e3b28f1 | 235 | /**************************************************************************/ |
Gaku0606 | 0:387c3e3b28f1 | 236 | /*! |
Gaku0606 | 0:387c3e3b28f1 | 237 | @brief Sets up the comparator to operate in basic mode, causing the |
Gaku0606 | 0:387c3e3b28f1 | 238 | ALERT/RDY pin to assert (go from high to low) when the ADC |
Gaku0606 | 0:387c3e3b28f1 | 239 | value exceeds the specified threshold. |
Gaku0606 | 0:387c3e3b28f1 | 240 | |
Gaku0606 | 0:387c3e3b28f1 | 241 | This will also set the ADC in continuous conversion mode. |
Gaku0606 | 0:387c3e3b28f1 | 242 | */ |
Gaku0606 | 0:387c3e3b28f1 | 243 | /**************************************************************************/ |
Gaku0606 | 0:387c3e3b28f1 | 244 | void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold) |
Gaku0606 | 0:387c3e3b28f1 | 245 | { |
Gaku0606 | 0:387c3e3b28f1 | 246 | // Start with default values |
Gaku0606 | 0:387c3e3b28f1 | 247 | uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match |
Gaku0606 | 0:387c3e3b28f1 | 248 | ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode |
Gaku0606 | 0:387c3e3b28f1 | 249 | ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) |
Gaku0606 | 0:387c3e3b28f1 | 250 | ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) |
Gaku0606 | 0:387c3e3b28f1 | 251 | ADS1015_REG_CONFIG_DR_3300SPS | // 1600(ADS1015) or 250(ADS1115) samples per second (default) |
Gaku0606 | 0:387c3e3b28f1 | 252 | ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode |
Gaku0606 | 0:387c3e3b28f1 | 253 | ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode |
Gaku0606 | 0:387c3e3b28f1 | 254 | |
Gaku0606 | 0:387c3e3b28f1 | 255 | // Set PGA/voltage range |
Gaku0606 | 0:387c3e3b28f1 | 256 | config |= m_gain; |
Gaku0606 | 0:387c3e3b28f1 | 257 | |
Gaku0606 | 0:387c3e3b28f1 | 258 | // Set single-ended input channel |
Gaku0606 | 0:387c3e3b28f1 | 259 | switch (channel) { |
Gaku0606 | 0:387c3e3b28f1 | 260 | case (0): |
Gaku0606 | 0:387c3e3b28f1 | 261 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_0; |
Gaku0606 | 0:387c3e3b28f1 | 262 | break; |
Gaku0606 | 0:387c3e3b28f1 | 263 | case (1): |
Gaku0606 | 0:387c3e3b28f1 | 264 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_1; |
Gaku0606 | 0:387c3e3b28f1 | 265 | break; |
Gaku0606 | 0:387c3e3b28f1 | 266 | case (2): |
Gaku0606 | 0:387c3e3b28f1 | 267 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_2; |
Gaku0606 | 0:387c3e3b28f1 | 268 | break; |
Gaku0606 | 0:387c3e3b28f1 | 269 | case (3): |
Gaku0606 | 0:387c3e3b28f1 | 270 | config |= ADS1015_REG_CONFIG_MUX_SINGLE_3; |
Gaku0606 | 0:387c3e3b28f1 | 271 | break; |
Gaku0606 | 0:387c3e3b28f1 | 272 | } |
Gaku0606 | 0:387c3e3b28f1 | 273 | |
Gaku0606 | 0:387c3e3b28f1 | 274 | // Set the high threshold register |
Gaku0606 | 0:387c3e3b28f1 | 275 | // Shift 12-bit results left 4 bits for the ADS1015 |
Gaku0606 | 0:387c3e3b28f1 | 276 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift); |
Gaku0606 | 0:387c3e3b28f1 | 277 | |
Gaku0606 | 0:387c3e3b28f1 | 278 | // Write config register to the ADC |
Gaku0606 | 0:387c3e3b28f1 | 279 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config); |
Gaku0606 | 0:387c3e3b28f1 | 280 | } |
Gaku0606 | 0:387c3e3b28f1 | 281 | |
Gaku0606 | 0:387c3e3b28f1 | 282 | /**************************************************************************/ |
Gaku0606 | 0:387c3e3b28f1 | 283 | /*! |
Gaku0606 | 0:387c3e3b28f1 | 284 | @brief In order to clear the comparator, we need to read the |
Gaku0606 | 0:387c3e3b28f1 | 285 | conversion results. This function reads the last conversion |
Gaku0606 | 0:387c3e3b28f1 | 286 | results without changing the config value. |
Gaku0606 | 0:387c3e3b28f1 | 287 | */ |
Gaku0606 | 0:387c3e3b28f1 | 288 | /**************************************************************************/ |
Gaku0606 | 0:387c3e3b28f1 | 289 | int16_t Adafruit_ADS1015::getLastConversionResults() |
Gaku0606 | 0:387c3e3b28f1 | 290 | { |
Gaku0606 | 0:387c3e3b28f1 | 291 | // Wait for the conversion to complete |
Gaku0606 | 0:387c3e3b28f1 | 292 | wait_ms(m_conversionDelay); |
Gaku0606 | 0:387c3e3b28f1 | 293 | |
Gaku0606 | 0:387c3e3b28f1 | 294 | // Read the conversion results |
Gaku0606 | 0:387c3e3b28f1 | 295 | uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift; |
Gaku0606 | 0:387c3e3b28f1 | 296 | if (m_bitShift == 0) { |
Gaku0606 | 0:387c3e3b28f1 | 297 | return (int16_t)res; |
Gaku0606 | 0:387c3e3b28f1 | 298 | } else { |
Gaku0606 | 0:387c3e3b28f1 | 299 | // Shift 12-bit results right 4 bits for the ADS1015, |
Gaku0606 | 0:387c3e3b28f1 | 300 | // making sure we keep the sign bit intact |
Gaku0606 | 0:387c3e3b28f1 | 301 | if (res > 0x07FF) { |
Gaku0606 | 0:387c3e3b28f1 | 302 | // negative number - extend the sign to 16th bit |
Gaku0606 | 0:387c3e3b28f1 | 303 | res |= 0xF000; |
Gaku0606 | 0:387c3e3b28f1 | 304 | } |
Gaku0606 | 0:387c3e3b28f1 | 305 | return (int16_t)res; |
Gaku0606 | 0:387c3e3b28f1 | 306 | } |
Gaku0606 | 0:387c3e3b28f1 | 307 | } |
Gaku0606 | 0:387c3e3b28f1 | 308 | |
Gaku0606 | 0:387c3e3b28f1 | 309 | void Adafruit_ADS1015::user(){ |
Gaku0606 | 0:387c3e3b28f1 | 310 | |
Gaku0606 | 0:387c3e3b28f1 | 311 | writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG , 0b0000001011101111);// |
Gaku0606 | 0:387c3e3b28f1 | 312 | |
Gaku0606 | 0:387c3e3b28f1 | 313 | } |