From f6d7a67683fd846bc7c9ac28bcd41e48af84498f Mon Sep 17 00:00:00 2001 From: cscherr Date: Mon, 28 Apr 2025 15:19:35 +0200 Subject: [PATCH] feat: try to read the temperature --- examples/temperature.rs | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 examples/temperature.rs diff --git a/examples/temperature.rs b/examples/temperature.rs new file mode 100755 index 0000000..442f6cb --- /dev/null +++ b/examples/temperature.rs @@ -0,0 +1,49 @@ +#![no_main] +#![no_std] + +use defmt::info; +use hal::adc::{Adc, Ready, VTemp}; +use hal::gpio::Analog; +use hal::gpio::gpiob::PB1; +use panic_probe as _; + +use defmt_rtt as _; // global logger + +use cortex_m_rt::entry; +use hal::{pac, prelude::*, rcc::Config}; + +const MAGIC_TEMPERATURE_NUMBER: f32 = 12.412122; + +#[entry] +fn main() -> ! { + let dp = pac::Peripherals::take().unwrap(); + let cp = cortex_m::Peripherals::take().unwrap(); + + let mut rcc = dp.RCC.freeze(Config::hsi16()); + let mut adc = dp.ADC.constrain(&mut rcc); + + let gpiob = dp.GPIOB.split(&mut rcc); + + let mut temp_pin = gpiob.pb1.into_analog(); + + // Get the delay provider. + let mut delay = cp.SYST.delay(rcc.clocks); + + let mut temp; + let mut i = 0; + loop { + if i % 10_000 == 0 { + temp = read_temp_c(&mut temp_pin, &mut adc); + info!("Temperature: {}", temp); + } + // delay.delay_ms(200_u16); + i += 1; + } +} + +fn read_temp_c(pin: &mut PB1, adc: &mut Adc) -> i16 { + let v: f32 = adc + .read(pin /* or maybe VTemp from the adc module? */) + .expect("could not read with adc"); + (v / MAGIC_TEMPERATURE_NUMBER) as i16 - 50 +}