further optimize ntc math

This commit is contained in:
Kilian Bracher 2025-03-27 16:53:11 +01:00
parent 29a48d22a7
commit 8a82ad4cee

View File

@ -1,18 +1,20 @@
#include <math.h>
#include <stdint.h>
#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 3000.0
#define VREF 3000.0f
// 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 int ntc_mv_to_celsius(int16_t adc) {
float log_ohms = logf(1/((VREF/adc)-1));
float log_ohms = logf(1 / ((VREF / adc) - 1));
return 1.0f / (NTC_A1 + NTC_B1 * log_ohms + NTC_C1 * log_ohms * log_ohms + NTC_D1 * log_ohms * log_ohms * log_ohms) - 273.15f;
}