diff --git a/src/temp_sensor.c b/src/temp_sensor.c index 2990ef7..0df956e 100644 --- a/src/temp_sensor.c +++ b/src/temp_sensor.c @@ -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 const fxp_t ln_r_ntc_nom = 705030; // ln(47kΩ) converted to fxp_t with 16 fractional bits - static const fxp_t r1 = FXP_FROM_INT(10000); + // note: all resistor values in kΩ! The factor 1000 is removed from the numbers! + 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 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 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 temp_k = fxp_div(FXP_FROM_INT(1.0), - fxp_div( - fxp_add( - fxp_div( - fxp_sub( - ln_r_ntc, - ln_r_ntc_nom), - b_constant), - FXP_FROM_INT(1)), - ntc_temp_nom)); + fxp_t temp_k = fxp_div(FXP_FROM_INT(1), + fxp_add( + fxp_div( + fxp_sub( + ln_r_ntc, + ln_r_ntc_nom), + b_constant), + ntc_temp_nom_inv)); return fxp_sub(temp_k, celsius2kelvin); }