Skip to content

Commit

Permalink
example: add f072 blocking_adc example
Browse files Browse the repository at this point in the history
  • Loading branch information
decaday committed Nov 23, 2024
1 parent 9d9e274 commit 1c95ea5
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/py32f072/src/bin/blocking_adc.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1c95ea5

Please sign in to comment.