From cf89fe2a72826c3a4dd4d058712e19fb61944613 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Wed, 12 Oct 2022 23:45:10 +0200 Subject: [PATCH] make max ADC value a per platform ADC function --- DCCTimer.h | 2 ++ DCCTimerAVR.cpp | 3 +++ DCCTimerESP.cpp | 3 +++ DCCTimerSAMD.cpp | 3 +++ MotorDriver.cpp | 9 +++------ 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/DCCTimer.h b/DCCTimer.h index 4733dbe..ed87e1f 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -111,6 +111,8 @@ public: // it was called from ISR because for some implementations that // makes a difference. static int read(uint8_t pin, bool fromISR=false); + // returns possible max value that the ADC can return + static int16_t ADCmax(); private: // On platforms that scan, it is called from waveform ISR // only on a regular basis. diff --git a/DCCTimerAVR.cpp b/DCCTimerAVR.cpp index 6759ac9..f29d1f3 100644 --- a/DCCTimerAVR.cpp +++ b/DCCTimerAVR.cpp @@ -144,6 +144,9 @@ int ADCee::init(uint8_t pin) { usedpins |= (1< ADC_INPUT_MAX_VALUE) { + if (rawCurrentTripValue + senseOffset > ADCee::ADCmax()) { // This would mean that the values obtained from the ADC never // can reach the trip value. So independent of the current, the // short circuit protection would never trip. So we adjust the @@ -117,8 +114,8 @@ MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, i // maximum value instead. // DIAG(F("Changing short detection value from %d to %d mA"), - // raw2mA(rawCurrentTripValue), raw2mA(ADC_INPUT_MAX_VALUE-senseOffset)); - rawCurrentTripValue=ADC_INPUT_MAX_VALUE-senseOffset; + // raw2mA(rawCurrentTripValue), raw2mA(ADCee::ADCmax()-senseOffset)); + rawCurrentTripValue=ADCee::ADCmax()-senseOffset; } if (currentPin==UNUSED_PIN)