44 lines
1.3 KiB
C

#include <math.h>
// For using the mV conversion makro
#include "ADBMS_Abstraction.h"
// #define USE_STEINHARDT
#ifdef USE_STEINHARDT
#define NTC_A1 0.003354016f // see NTC RT csv from Vishay
#define NTC_B1 0.000300131f
#define NTC_C1 5.08516E-06f
#define NTC_D1 2.18765E-07f
#define VREF_mV 3000.0f // 3V
#define TEMP_CONV 1000.0f // 65.535°C max; maybe 100 for better safety against overflow
#define CELSIUS_TO_KELVIN 273.15f
#define CELSIUS_TO_KELVIN_SCALED CELSIUS_TO_KELVIN * TEMP_CONV
// More efficient (?) calc using:
// R_T = R_Pullup / (V_REF / ADC - 1)
// With R_T/R_0 and R_0 = R_T@25C
// R_T/R_0 = 1 / V_REF / ADC - 1
[[gnu::optimize("fast-math")]]
static inline uint16_t ntc_adc_to_celsius(int16_t adc_codes) {
// Calculate voltage from ADC
int16_t adc_mV = mV_from_ADBMS6830(adc_codes);
float log_ohms = logf(1 / ((VREF_mV/adc_mV) - 1));
return (uint16_t) (TEMP_CONV / (NTC_A1 + NTC_B1 * log_ohms + NTC_C1 * log_ohms * log_ohms + NTC_D1 * log_ohms * log_ohms * log_ohms) - CELSIUS_TO_KELVIN_SCALED);
}
#else
#include "tempTable.h"
static inline uint16_t ntc_adc_to_celsius(int16_t adc_codes) {
// Out of bounds checking --> done by OV/UV setting of the BMS?
if (adc_codes < -6355 || adc_codes > 4640) {
// should trigger overtemp, right?
return -1;
}
return ADC2TEMP[adc_codes + 6355];
}
#endif