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

41 lines
1.1 KiB
Rust
Raw Normal View History

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::adc::Adc;
2023-09-28 03:58:46 +02:00
use embassy_stm32::peripherals::ADC1;
use embassy_stm32::{adc, bind_interrupts};
use embassy_time::{Delay, Duration, Timer};
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
2023-09-28 03:58:46 +02:00
bind_interrupts!(struct Irqs {
ADC1_2 => adc::InterruptHandler<ADC1>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
let mut adc = Adc::new(p.ADC1, &mut Delay);
let mut pin = p.PB1;
2022-10-24 22:01:16 +02:00
let mut vrefint = adc.enable_vref(&mut Delay);
2023-09-28 03:58:46 +02:00
let vrefint_sample = adc.read(&mut vrefint).await;
let convert_to_millivolts = |sample| {
// From http://www.st.com/resource/en/datasheet/CD00161566.pdf
// 5.3.4 Embedded reference voltage
2022-10-24 22:01:16 +02:00
const VREFINT_MV: u32 = 1200; // mV
2022-10-24 22:01:16 +02:00
(u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16
};
loop {
2023-09-28 03:58:46 +02:00
let v = adc.read(&mut pin).await;
info!("--> {} - {} mV", v, convert_to_millivolts(v));
Timer::after(Duration::from_millis(100)).await;
}
}