From c5efc6ca57488696b2ac6435a9128aa1798ffef5 Mon Sep 17 00:00:00 2001 From: linuswck Date: Tue, 23 Apr 2024 15:35:23 +0800 Subject: [PATCH] MAX1968: Improve STM32 ADC calibration consistency --- src/thermostat/max1968.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/thermostat/max1968.rs b/src/thermostat/max1968.rs index 9221f85..13fe147 100644 --- a/src/thermostat/max1968.rs +++ b/src/thermostat/max1968.rs @@ -125,7 +125,17 @@ impl MAX1968 { .default_sample_time(config::SampleTime::Cycles_480); // Do not set reset RCCs as it causes other ADCs' clock to be disabled let mut pins_adc1 = Adc::adc1(adc1, false, adc_config); - pins_adc1.calibrate(); + + // adc1.calibrate() fn only read REFINT once to assign the calibration value. + // It does not take the STM32F4's ADC Precision Limitation into account. + // AN4073: ADC Reading Dispersion can be reduced through Averaging + let mut vdda_mv: u32 = 0; + for _ in (0..512).rev() { + pins_adc1.calibrate(); + vdda_mv += pins_adc1.reference_voltage(); + } + vdda_mv = vdda_mv / 512 as u32; + pins_adc1.apply_config(adc_config.reference_voltage(vdda_mv)); let adc_config = AdcConfig::default() .clock(config::Clock::Pclk2_div_8)