Fixed the NTC temperature calculation

This commit is contained in:
Thomas Kolb 2023-09-29 20:50:16 +02:00
parent f5756a4f64
commit 12b5c8f7f4

View file

@ -46,10 +46,11 @@ static fxp_t adc_val_to_pin_voltage(uint16_t adc_val)
static fxp_t calc_temperature_ntc(uint16_t adc_val) static fxp_t calc_temperature_ntc(uint16_t adc_val)
{ {
static const fxp_t ln_r_ntc_nom = 705030; // ln(47kΩ) converted to fxp_t with 16 fractional bits // note: all resistor values in kΩ! The factor 1000 is removed from the numbers!
static const fxp_t r1 = FXP_FROM_INT(10000); static const fxp_t ln_r_ntc_nom = 252323; // ln(47 kΩ) converted to fxp_t with 16 fractional bits
static const fxp_t r1 = FXP_FROM_INT(10);
static const fxp_t b_constant = FXP_FROM_INT(4125); static const fxp_t b_constant = FXP_FROM_INT(4125);
static const fxp_t ntc_temp_nom = 19539558; // (273.15+25) * 2**16 static const fxp_t ntc_temp_nom_inv = 220; // 1/(273.15+25) * 2**16
static const fxp_t celsius2kelvin = 17901158; // (273.15) * 2**16 static const fxp_t celsius2kelvin = 17901158; // (273.15) * 2**16
fxp_t v_r1 = adc_val_to_pin_voltage(adc_val); fxp_t v_r1 = adc_val_to_pin_voltage(adc_val);
@ -59,16 +60,14 @@ static fxp_t calc_temperature_ntc(uint16_t adc_val)
fxp_t ln_r_ntc = fxp_from_float(logf(fxp_to_float(r_ntc))); fxp_t ln_r_ntc = fxp_from_float(logf(fxp_to_float(r_ntc)));
fxp_t temp_k = fxp_div(FXP_FROM_INT(1.0), fxp_t temp_k = fxp_div(FXP_FROM_INT(1),
fxp_div(
fxp_add( fxp_add(
fxp_div( fxp_div(
fxp_sub( fxp_sub(
ln_r_ntc, ln_r_ntc,
ln_r_ntc_nom), ln_r_ntc_nom),
b_constant), b_constant),
FXP_FROM_INT(1)), ntc_temp_nom_inv));
ntc_temp_nom));
return fxp_sub(temp_k, celsius2kelvin); return fxp_sub(temp_k, celsius2kelvin);
} }