slightly better temp accuracy with midpoint at 25c

This commit is contained in:
JP Stringham
2025-12-26 19:08:37 -05:00
parent 459fd79123
commit 5576b8ca55

View File

@@ -39,7 +39,11 @@ pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
/// if your board has a different frequency
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
// ADC readings at various temps (calibration)
// we linearize between these points,
// so more points can help maximize accuracy
const ZERO_DEG_C: u16 = 740;
const TWENTY_FIVE_C: u16 = 1750;
const SIXTY_C: u16 = 3200;
/// Entry point to our bare-metal application.
@@ -95,18 +99,26 @@ fn main() -> ! {
loop {
led_pin.set_high().unwrap();
timer.delay_ms(200);
timer.delay_ms(300);
led_pin.set_low().unwrap();
timer.delay_ms(200);
timer.delay_ms(300);
let adc_val = adc.read(&mut adc_pin).unwrap();
println!("ADC {=u16}, samp {=u16}", adc_val, test_val);
let mut deg = adc_val.saturating_sub(ZERO_DEG_C) as f32;
deg = (deg * 60.) / (SIXTY_C - ZERO_DEG_C) as f32;
let deg = match adc_val {
..ZERO_DEG_C => 0f32,
ZERO_DEG_C..=TWENTY_FIVE_C => {
let d = (adc_val - ZERO_DEG_C) as f32;
(d * 25.) / (TWENTY_FIVE_C - ZERO_DEG_C) as f32
}
_ => {
let d = (adc_val - TWENTY_FIVE_C) as f32;
25f32 + (d * 35.) / (SIXTY_C - TWENTY_FIVE_C) as f32
}
};
let deg = (deg * 100.) as u32;
let deg = (deg as f32) / 100.;
let deg = (deg * 10.) as u32;
let deg = (deg as f32) / 10.;
println!("Deg? {=f32}", deg);