From 6adeb07209e7f6c4300653dbf3be1c40107e9b4c Mon Sep 17 00:00:00 2001 From: "Wolfgang (Wolle) Ewald" Date: Thu, 2 Jul 2020 21:36:50 +0200 Subject: Add files via upload --- src/ADS1115_WE.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/ADS1115_WE.cpp') diff --git a/src/ADS1115_WE.cpp b/src/ADS1115_WE.cpp index dc3098b..b7f3918 100644 --- a/src/ADS1115_WE.cpp +++ b/src/ADS1115_WE.cpp @@ -37,6 +37,8 @@ bool ADS1115_WE::init(){ } 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; } @@ -88,6 +90,7 @@ void ADS1115_WE::setMeasureMode(ADS1115_MEASURE_MODE mode){ } void ADS1115_WE::setVoltageRange_mV(ADS1115_RANGE range){ + uint16_t currentVoltageRange = voltageRange; switch(range){ case ADS1115_RANGE_6144: @@ -115,19 +118,20 @@ void ADS1115_WE::setVoltageRange_mV(ADS1115_RANGE range){ uint16_t currentAlertPinMode = currentConfReg & 3; uint16_t currentCompMode = (currentConfReg>>4) & 1; + currentConfReg &= ~(0x0E00); + currentConfReg |= range; + writeRegister(ADS1115_CONFIG_REG, currentConfReg); + if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){ int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG); - alertLimit = (alertLimit/currentRange) * range; + alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); writeRegister(ADS1115_HI_THRESH_REG, alertLimit); alertLimit = readRegister(ADS1115_LO_THRESH_REG); - alertLimit = (alertLimit/currentRange) * range; + alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); writeRegister(ADS1115_LO_THRESH_REG, alertLimit); } - currentConfReg &= ~(0x0E00); - currentConfReg |= range; - writeRegister(ADS1115_CONFIG_REG, currentConfReg); } void ADS1115_WE::setCompareChannels(ADS1115_MUX mux){ -- cgit v1.2.3