refactor: move NTC calc functions to their own file

This commit is contained in:
Kilian Bracher 2025-03-26 17:25:23 +01:00
parent 925ecd9671
commit 11d5ccb2d3
2 changed files with 28 additions and 25 deletions

View File

@ -0,0 +1,27 @@
#include <math.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 NTC_R25 10000.0f // 10k ohm at 25C
#define NTC_R_DIVIDER 10000.0f
#define NTC_V_DIVIDER 3.000f // 3V
[[gnu::always_inline]]
static inline float ntc_ohms_to_celsius(float ohms) {
float log_ohms = logf(ohms / NTC_R25);
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;
}
[[gnu::always_inline]]
static inline float ohms_from_ntc_v(float v) {
return (NTC_R_DIVIDER * v) / (NTC_V_DIVIDER - v);
}
[[maybe_unused]]
static int ntc_mv_to_celsius(int mv) {
float v = (float)mv * 0.001f;
return (int)ntc_ohms_to_celsius(ohms_from_ntc_v(v));
}

View File

@ -25,6 +25,7 @@
#include "config_ADBMS6830.h"
#include "swo_log.h"
#include <string.h>
#include "NTC.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -62,31 +63,6 @@ static void MX_SPI1_Init(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t volatile logging_mask = 0b11111; // no LOG_LEVEL_NOISY
#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 NTC_R25 10000.0f // 10k ohm at 25C
#define NTC_R_DIVIDER 10000.0f
#define NTC_V_DIVIDER 3.000f // 3V
[[gnu::always_inline]]
static inline float ntc_ohms_to_celsius(float ohms) {
float log_ohms = logf(ohms / NTC_R25);
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;
}
[[gnu::always_inline]]
static inline float ohms_from_ntc_v(float v) {
return (NTC_R_DIVIDER * v) / (NTC_V_DIVIDER - v);
}
static inline int ntc_mv_to_celsius(int mv) {
float v = (float)mv * 0.001f;
return (int)ntc_ohms_to_celsius(ohms_from_ntc_v(v));
}
/* USER CODE END 0 */
/**