49 lines
1.2 KiB
Rust
Executable file
49 lines
1.2 KiB
Rust
Executable file
#![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
|
|
}
|