diff options
Diffstat (limited to 'src/ADS1115_WE.cpp')
-rw-r--r-- | src/ADS1115_WE.cpp | 326 |
1 files changed, 202 insertions, 124 deletions
diff --git a/src/ADS1115_WE.cpp b/src/ADS1115_WE.cpp index e771f08..d9547ad 100644 --- a/src/ADS1115_WE.cpp +++ b/src/ADS1115_WE.cpp @@ -17,193 +17,271 @@ #include "ADS1115_WE.h"
ADS1115_WE::ADS1115_WE(int addr){
- i2cAddress = addr;
+ i2cAddress = addr;
}
ADS1115_WE::ADS1115_WE(){
- i2cAddress = 0x48;
+ i2cAddress = 0x48;
}
void ADS1115_WE::reset(){
- Wire.beginTransmission(0);
- Wire.write(0x06);
- Wire.endTransmission();
+ Wire.beginTransmission(0);
+ Wire.write(0x06);
+ Wire.endTransmission();
}
-bool ADS1115_WE::init(){
- Wire.beginTransmission(i2cAddress);
- uint8_t success = Wire.endTransmission();
- if(success){
- return 0;
- }
- writeRegister(ADS1115_CONFIG_REG, ADS1115_REG_RESET_VAL);
- setVoltageRange_mV(ADS1115_RANGE_2048);
- writeRegister(ADS1115_LO_THRESH_REG, 0x8000);
- writeRegister(ADS1115_HI_THRESH_REG, 0x7FFF);
- return 1;
+bool ADS1115_WE::init(){
+ Wire.beginTransmission(i2cAddress);
+ uint8_t success = Wire.endTransmission();
+ if(success){
+ return 0;
+ }
+ writeRegister(ADS1115_CONFIG_REG, ADS1115_REG_RESET_VAL);
+ setVoltageRange_mV(ADS1115_RANGE_2048);
+ writeRegister(ADS1115_LO_THRESH_REG, 0x8000);
+ writeRegister(ADS1115_HI_THRESH_REG, 0x7FFF);
+ deviceMeasureMode = ADS1115_SINGLE;
+ return 1;
}
void ADS1115_WE::setAlertPinMode(ADS1115_COMP_QUE mode){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x0003);
- currentConfReg |= mode;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0003);
+ currentConfReg |= mode;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
}
void ADS1115_WE::setAlertLatch(ADS1115_LATCH latch){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x0004);
- currentConfReg |= latch;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0004);
+ currentConfReg |= latch;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
}
void ADS1115_WE::setAlertPol(ADS1115_ALERT_POL polarity){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x0008);
- currentConfReg |= polarity;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0008);
+ currentConfReg |= polarity;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
}
void ADS1115_WE::setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hiThres, float loThres){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x0010);
- currentConfReg |= mode;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
- int16_t alertLimit = calcLimit(hiThres);
- writeRegister(ADS1115_HI_THRESH_REG, alertLimit);
- alertLimit = calcLimit(loThres);
- writeRegister(ADS1115_LO_THRESH_REG, alertLimit);
-
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0010);
+ currentConfReg |= mode;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ int16_t alertLimit = calcLimit(hiThres);
+ writeRegister(ADS1115_HI_THRESH_REG, alertLimit);
+ alertLimit = calcLimit(loThres);
+ writeRegister(ADS1115_LO_THRESH_REG, alertLimit);
+
}
void ADS1115_WE::setConvRate(ADS1115_CONV_RATE rate){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x00E0);
- currentConfReg |= rate;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x00E0);
+ currentConfReg |= rate;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
}
-
+
+convRate ADS1115_WE::getConvRate(){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ return (convRate)(currentConfReg & 0xE0);
+}
+
void ADS1115_WE::setMeasureMode(ADS1115_MEASURE_MODE mode){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x0100);
- currentConfReg |= mode;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ deviceMeasureMode = mode;
+ currentConfReg &= ~(0x0100);
+ currentConfReg |= mode;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
}
void ADS1115_WE::setVoltageRange_mV(ADS1115_RANGE range){
- uint16_t currentVoltageRange = voltageRange;
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- uint16_t currentRange = (currentConfReg >> 9) & 7;
- uint16_t currentAlertPinMode = currentConfReg & 3;
-
- setMeasureMode(ADS1115_SINGLE);
-
- switch(range){
- case ADS1115_RANGE_6144:
- voltageRange = 6144;
- break;
- case ADS1115_RANGE_4096:
- voltageRange = 4096;
- break;
- case ADS1115_RANGE_2048:
- voltageRange = 2048;
- break;
- case ADS1115_RANGE_1024:
- voltageRange = 1024;
- break;
- case ADS1115_RANGE_0512:
- voltageRange = 512;
- break;
- case ADS1115_RANGE_0256:
- voltageRange = 256;
- break;
- }
-
- if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){
- int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG);
- alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange);
- writeRegister(ADS1115_HI_THRESH_REG, alertLimit);
-
- alertLimit = readRegister(ADS1115_LO_THRESH_REG);
- alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange);
- writeRegister(ADS1115_LO_THRESH_REG, alertLimit);
- }
-
- currentConfReg &= ~(0x0E00);
- currentConfReg |= range;
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentVoltageRange = voltageRange;
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ uint16_t currentRange = (currentConfReg >> 9) & 7;
+ uint16_t currentAlertPinMode = currentConfReg & 3;
+
+ setMeasureMode(ADS1115_SINGLE);
+
+ switch(range){
+ case ADS1115_RANGE_6144:
+ voltageRange = 6144;
+ break;
+ case ADS1115_RANGE_4096:
+ voltageRange = 4096;
+ break;
+ case ADS1115_RANGE_2048:
+ voltageRange = 2048;
+ break;
+ case ADS1115_RANGE_1024:
+ voltageRange = 1024;
+ break;
+ case ADS1115_RANGE_0512:
+ voltageRange = 512;
+ break;
+ case ADS1115_RANGE_0256:
+ voltageRange = 256;
+ break;
+ }
+
+ if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){
+ int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG);
+ alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange);
+ writeRegister(ADS1115_HI_THRESH_REG, alertLimit);
+
+ alertLimit = readRegister(ADS1115_LO_THRESH_REG);
+ alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange);
+ writeRegister(ADS1115_LO_THRESH_REG, alertLimit);
+ }
+
+ currentConfReg &= ~(0x0E00);
+ currentConfReg |= range;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setAutoRange(){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ convRate rate = getConvRate();
+ setVoltageRange_mV(ADS1115_RANGE_6144);
+ delayAccToRate(rate);
+
+ if(deviceMeasureMode == ADS1115_SINGLE){
+ setMeasureMode(ADS1115_CONTINUOUS);
+ delayAccToRate(rate);
+ }
+
+ float result = abs(getResult_mV());
+ range optRange = ADS1115_RANGE_6144;
+
+ if(result < 205.0){
+ optRange = ADS1115_RANGE_0256;
+ }
+ else if(result < 410.0){
+ optRange = ADS1115_RANGE_0512;
+ }
+ else if(result < 820.0){
+ optRange = ADS1115_RANGE_1024;
+ }
+ else if(result < 1640.0){
+ optRange = ADS1115_RANGE_2048;
+ }
+ else if(result < 3280.0){
+ optRange = ADS1115_RANGE_4096;
+ }
+
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ setVoltageRange_mV(optRange);
+ delayAccToRate(rate);
+}
+
+void ADS1115_WE::delayAccToRate(convRate cr){
+ switch(cr){
+ case ADS1115_8_SPS:
+ delay(130);
+ break;
+ case ADS1115_16_SPS:
+ delay(65);
+ break;
+ case ADS1115_32_SPS:
+ delay(32);
+ break;
+ case ADS1115_64_SPS:
+ delay(16);
+ break;
+ case ADS1115_128_SPS:
+ delay(8);
+ break;
+ case ADS1115_250_SPS:
+ delay(4);
+ break;
+ case ADS1115_475_SPS:
+ delay(3);
+ break;
+ case ADS1115_860_SPS:
+ delay(2);
+ break;
+ }
}
+
void ADS1115_WE::setCompareChannels(ADS1115_MUX mux){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg &= ~(0x7000);
- currentConfReg |= (mux);
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x7000);
+ currentConfReg |= (mux);
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+
+ if(!(currentConfReg & 0x0100)){ // => if not single shot mode
+ convRate rate = getConvRate();
+ delayAccToRate(rate);
+ delayAccToRate(rate);
+ }
}
-
+
bool ADS1115_WE::isBusy(){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- return (!(currentConfReg>>15) & 1);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ return (!(currentConfReg>>15) & 1);
}
-
+
void ADS1115_WE::startSingleMeasurement(){
- uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
- currentConfReg |= (1 << 15);
- writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg |= (1 << 15);
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
}
-
+
float ADS1115_WE::getResult_V(){
- int16_t rawResult = readRegister(ADS1115_CONV_REG);
- float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange/1000;
- return result;
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange/1000;
+ return result;
}
float ADS1115_WE::getResult_mV(){
- int16_t rawResult = readRegister(ADS1115_CONV_REG);
- float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange;
- return result;
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange;
+ return result;
}
int16_t ADS1115_WE::getRawResult(){
- int16_t rawResult = readRegister(ADS1115_CONV_REG);
- return rawResult;
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ return rawResult;
}
int16_t ADS1115_WE::getResultWithRange(int16_t min, int16_t max){
- int16_t rawResult = readRegister(ADS1115_CONV_REG);
- int16_t result = 0;
- result = map(rawResult, -32767, 32767, min, max);
- return result;
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ int16_t result = 0;
+ result = map(rawResult, -32767, 32767, min, max);
+ return result;
}
int16_t ADS1115_WE::getResultWithRange(int16_t min, int16_t max, int16_t maxMillivolt){
- int16_t rawResult = readRegister(ADS1115_CONV_REG);
- int16_t result = 0;
- result = map(rawResult, -32767, 32767, min, max);
- result = (int16_t) ((1.0 * result * voltageRange / maxMillivolt) + 0.5);
- return result;
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ int16_t result = 0;
+ result = map(rawResult, -32767, 32767, min, max);
+ result = (int16_t) ((1.0 * result * voltageRange / maxMillivolt) + 0.5);
+ return result;
}
uint16_t ADS1115_WE::getVoltageRange_mV(){
- return voltageRange;
+ return voltageRange;
}
void ADS1115_WE::setAlertPinToConversionReady(){
- writeRegister(ADS1115_LO_THRESH_REG, (0<<15));
- writeRegister(ADS1115_HI_THRESH_REG, (1<<15));
+ writeRegister(ADS1115_LO_THRESH_REG, (0<<15));
+ writeRegister(ADS1115_HI_THRESH_REG, (1<<15));
}
void ADS1115_WE::clearAlert(){
- readRegister(ADS1115_CONV_REG);
+ readRegister(ADS1115_CONV_REG);
}
/************************************************
- private functions
+ private functions
*************************************************/
int16_t ADS1115_WE::calcLimit(float rawLimit){
- int16_t limit = (int16_t)((rawLimit * ADS1115_REG_FACTOR / voltageRange)*1000);
- return limit;
+ int16_t limit = (int16_t)((rawLimit * ADS1115_REG_FACTOR / voltageRange)*1000);
+ return limit;
}
uint8_t ADS1115_WE::writeRegister(uint8_t reg, uint16_t val){
@@ -230,6 +308,6 @@ uint16_t ADS1115_WE::readRegister(uint8_t reg){ regValue = (MSByte<<8) + LSByte;
return regValue;
}
-
+
|