feat: try to read the temperature
This commit is contained in:
parent
6fb993300e
commit
f6d7a67683
1 changed files with 49 additions and 0 deletions
49
examples/temperature.rs
Executable file
49
examples/temperature.rs
Executable 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
|
||||
}
|
Loading…
Add table
Reference in a new issue