feat: try to read the temperature

This commit is contained in:
cscherr 2025-04-28 15:19:35 +02:00
parent 6fb993300e
commit f6d7a67683
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7

49
examples/temperature.rs Executable file
View file

@ -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<Analog>, adc: &mut Adc<Ready>) -> 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
}