embassy/examples/stm32f334/src/bin/adc.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

2023-09-05 23:46:57 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::info;
use embassy_executor::Spawner;
2023-09-10 05:01:51 +02:00
use embassy_stm32::adc::{Adc, VREF_INT};
2023-09-05 23:46:57 +02:00
use embassy_stm32::rcc::{ADCClock, ADCPrescaler};
use embassy_stm32::time::Hertz;
use embassy_stm32::Config;
use embassy_time::{Delay, Duration, Timer};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
let mut config = Config::default();
config.rcc.hse = Some(Hertz(8_000_000));
config.rcc.sysclk = Some(Hertz(16_000_000));
config.rcc.adc = Some(ADCClock::PLL(ADCPrescaler::Div1));
let mut p = embassy_stm32::init(config);
2023-09-10 05:01:51 +02:00
info!("create adc...");
2023-09-05 23:46:57 +02:00
let mut adc = Adc::new(p.ADC1, &mut Delay);
2023-09-10 05:01:51 +02:00
info!("enable vrefint...");
2023-09-05 23:46:57 +02:00
2023-09-10 05:01:51 +02:00
let mut vrefint = adc.enable_vref(&mut Delay);
let mut temperature = adc.enable_temperature();
2023-09-05 23:46:57 +02:00
loop {
2023-09-10 05:01:51 +02:00
let vref = adc.read(&mut vrefint);
info!("read vref: {}", vref);
let temp = adc.read(&mut temperature);
info!("read temperature: {}", temp);
let pin = adc.read(&mut p.PA0);
info!("read pin: {}", pin);
let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32;
info!("computed pin mv: {}", pin_mv);
2023-09-05 23:46:57 +02:00
Timer::after(Duration::from_secs(1)).await;
}
}