diff --git a/examples/py32f072/src/bin/blocking_adc.rs b/examples/py32f072/src/bin/blocking_adc.rs new file mode 100644 index 0000000..9d67896 --- /dev/null +++ b/examples/py32f072/src/bin/blocking_adc.rs @@ -0,0 +1,48 @@ +#![no_std] +#![no_main] +#![feature(impl_trait_in_assoc_type)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_time::Timer; +use py32_hal::rcc::{Pll, PllSource, Sysclk, PllMul}; +use py32_hal::time::Hertz; +use py32_hal::adc::{Adc, SampleTime, Prescaler}; +use py32_hal::peripherals::ADC; +use py32_hal::adc; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let mut cfg: py32_hal::Config = Default::default(); + cfg.rcc.hsi = Some(Hertz::mhz(24)); + cfg.rcc.pll = Some(Pll { + src: PllSource::HSI, + mul: PllMul::MUL3, + }); + cfg.rcc.sys = Sysclk::PLL; + let p = py32_hal::init(cfg); + + info!("Hello World!"); + + let mut adc = Adc::new(p.ADC, Prescaler::Div4); + adc.set_sample_time(SampleTime::CYCLES28_5); + let mut pin = p.PA7; + + let mut vrefint = adc.enable_vrefint(); + + loop { + let vrefint_sample = adc.blocking_read(&mut vrefint); + let v = adc.blocking_read(&mut pin); + info!("value: {}", v); + info!("vrefint_sample: {}", vrefint_sample); + info!("--> {} - {} mV", v, convert_to_millivolts(v, vrefint_sample)); + Timer::after_millis(100).await; + } +} + +pub fn convert_to_millivolts(sample: u16, vrefint: u16) -> u16 { + const VREFINT_MV: u32 = 1200; // mV + + (u32::from(sample) * VREFINT_MV / u32::from(vrefint)) as u16 +} \ No newline at end of file