-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: add f072 blocking_adc example
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |