stm32/adc: add async conversion
This commit is contained in:
@ -4,13 +4,18 @@
|
||||
|
||||
use defmt::info;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_stm32::adc::{Adc, SampleTime, VREF_INT};
|
||||
use embassy_stm32::adc::{Adc, SampleTime};
|
||||
use embassy_stm32::peripherals::ADC1;
|
||||
use embassy_stm32::rcc::AdcClockSource;
|
||||
use embassy_stm32::time::mhz;
|
||||
use embassy_stm32::Config;
|
||||
use embassy_stm32::{adc, bind_interrupts, Config};
|
||||
use embassy_time::{Delay, Duration, Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
ADC1_2 => adc::InterruptHandler<ADC1>;
|
||||
});
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(_spawner: Spawner) -> ! {
|
||||
let mut config = Config::default();
|
||||
@ -24,7 +29,7 @@ async fn main(_spawner: Spawner) -> ! {
|
||||
|
||||
info!("create adc...");
|
||||
|
||||
let mut adc = Adc::new(p.ADC1, &mut Delay);
|
||||
let mut adc = Adc::new(p.ADC1, Irqs, &mut Delay);
|
||||
|
||||
adc.set_sample_time(SampleTime::Cycles601_5);
|
||||
|
||||
@ -34,18 +39,18 @@ async fn main(_spawner: Spawner) -> ! {
|
||||
let mut temperature = adc.enable_temperature();
|
||||
|
||||
loop {
|
||||
let vref = adc.read(&mut vrefint);
|
||||
info!("read vref: {}", vref);
|
||||
let vref = adc.read(&mut vrefint).await;
|
||||
info!("read vref: {} (should be {})", vref, vrefint.value());
|
||||
|
||||
let temp = adc.read(&mut temperature);
|
||||
let temp = adc.read(&mut temperature).await;
|
||||
info!("read temperature: {}", temp);
|
||||
|
||||
let pin = adc.read(&mut p.PA0);
|
||||
let pin = adc.read(&mut p.PA0).await;
|
||||
info!("read pin: {}", pin);
|
||||
|
||||
let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32;
|
||||
let pin_mv = (pin as u32 * vrefint.value() as u32 / vref as u32) * 3300 / 4095;
|
||||
info!("computed pin mv: {}", pin_mv);
|
||||
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user