From 49ac1ce37bfaf3095d18138763bb536aa46d2087 Mon Sep 17 00:00:00 2001 From: "Wolfgang (Wolle) Ewald" Date: Wed, 21 Apr 2021 20:21:42 +0200 Subject: Add files via upload --- src/ADS1115_WE.cpp | 41 +++++++++++++++++++++++++++++++---------- src/ADS1115_WE.h | 7 +++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/ADS1115_WE.cpp b/src/ADS1115_WE.cpp index 01cb4fa..47ed03d 100644 --- a/src/ADS1115_WE.cpp +++ b/src/ADS1115_WE.cpp @@ -41,6 +41,7 @@ bool ADS1115_WE::init(){ writeRegister(ADS1115_LO_THRESH_REG, 0x8000); writeRegister(ADS1115_HI_THRESH_REG, 0x7FFF); deviceMeasureMode = ADS1115_SINGLE; + autoRangeMode = false; return 1; } @@ -153,22 +154,22 @@ void ADS1115_WE::setAutoRange(){ delayAccToRate(rate); } - float result = abs(getResult_mV()); + int16_t rawResult = abs(readRegister(ADS1115_CONV_REG)); range optRange = ADS1115_RANGE_6144; - if(result < 205.0){ + if(rawResult < 1093){ optRange = ADS1115_RANGE_0256; } - else if(result < 410.0){ + else if(rawResult < 2185){ optRange = ADS1115_RANGE_0512; } - else if(result < 820.0){ + else if(rawResult < 4370){ optRange = ADS1115_RANGE_1024; } - else if(result < 1640.0){ + else if(rawResult < 8738){ optRange = ADS1115_RANGE_2048; } - else if(result < 3280.0){ + else if(rawResult < 17476){ optRange = ADS1115_RANGE_4096; } @@ -176,6 +177,16 @@ void ADS1115_WE::setAutoRange(){ setVoltageRange_mV(optRange); } +void ADS1115_WE::setPermanentAutoRangeMode(bool autoMode){ + if(autoMode){ + autoRangeMode = true; + } + else{ + autoRangeMode = false; + } +} + + void ADS1115_WE::delayAccToRate(convRate cr){ switch(cr){ case ADS1115_8_SPS: @@ -237,31 +248,41 @@ void ADS1115_WE::startSingleMeasurement(){ } float ADS1115_WE::getResult_V(){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); + int16_t rawResult = getRawResult(); float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange/1000; return result; } float ADS1115_WE::getResult_mV(){ - int16_t rawResult = readRegister(ADS1115_CONV_REG); + int16_t rawResult = getRawResult(); float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange; return result; } int16_t ADS1115_WE::getRawResult(){ int16_t rawResult = readRegister(ADS1115_CONV_REG); + if(autoRangeMode){ + if((abs(rawResult) > 26214) && (voltageRange != 6144)){ // 80% + setAutoRange(); + rawResult = readRegister(ADS1115_CONV_REG); + } + else if((abs(rawResult) < 9800) && (voltageRange != 256)){ //30% + setAutoRange(); + 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 rawResult = getRawResult(); 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 rawResult = getRawResult(); int16_t result = 0; result = map(rawResult, -32767, 32767, min, max); result = (int16_t) ((1.0 * result * voltageRange / maxMillivolt) + 0.5); diff --git a/src/ADS1115_WE.h b/src/ADS1115_WE.h index 395d4d0..3af248b 100644 --- a/src/ADS1115_WE.h +++ b/src/ADS1115_WE.h @@ -200,6 +200,12 @@ public: * You should ony use it in case you expect stable or slowly changing voltages. */ void setAutoRange(); + + /* Set the automatic voltage range permanantly, but the range will only be changed if the + * measured value is outside 30 - 80% of the maximum value of the current range. + * Therefore this method is faster than setAutoRange(). + */ + void setPermanentAutoRangeMode(bool autoMode); /* Set the inputs to be compared * @@ -263,6 +269,7 @@ private: uint16_t voltageRange; ADS1115_MEASURE_MODE deviceMeasureMode; int i2cAddress; + bool autoRangeMode; void delayAccToRate(convRate cr); int16_t calcLimit(float rawLimit); uint8_t writeRegister(uint8_t reg, uint16_t val); -- cgit v1.2.3