diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f334/src/bin/adc.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/examples/stm32f334/src/bin/adc.rs b/examples/stm32f334/src/bin/adc.rs index e8b2cd8a7..ed246a7db 100644 --- a/examples/stm32f334/src/bin/adc.rs +++ b/examples/stm32f334/src/bin/adc.rs | |||
| @@ -4,13 +4,18 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::info; | 5 | use defmt::info; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::adc::{Adc, SampleTime, VREF_INT}; | 7 | use embassy_stm32::adc::{Adc, SampleTime}; |
| 8 | use embassy_stm32::peripherals::ADC1; | ||
| 8 | use embassy_stm32::rcc::AdcClockSource; | 9 | use embassy_stm32::rcc::AdcClockSource; |
| 9 | use embassy_stm32::time::mhz; | 10 | use embassy_stm32::time::mhz; |
| 10 | use embassy_stm32::Config; | 11 | use embassy_stm32::{adc, bind_interrupts, Config}; |
| 11 | use embassy_time::{Delay, Duration, Timer}; | 12 | use embassy_time::{Delay, Duration, Timer}; |
| 12 | use {defmt_rtt as _, panic_probe as _}; | 13 | use {defmt_rtt as _, panic_probe as _}; |
| 13 | 14 | ||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | ADC1_2 => adc::InterruptHandler<ADC1>; | ||
| 17 | }); | ||
| 18 | |||
| 14 | #[embassy_executor::main] | 19 | #[embassy_executor::main] |
| 15 | async fn main(_spawner: Spawner) -> ! { | 20 | async fn main(_spawner: Spawner) -> ! { |
| 16 | let mut config = Config::default(); | 21 | let mut config = Config::default(); |
| @@ -24,7 +29,7 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 24 | 29 | ||
| 25 | info!("create adc..."); | 30 | info!("create adc..."); |
| 26 | 31 | ||
| 27 | let mut adc = Adc::new(p.ADC1, &mut Delay); | 32 | let mut adc = Adc::new(p.ADC1, Irqs, &mut Delay); |
| 28 | 33 | ||
| 29 | adc.set_sample_time(SampleTime::Cycles601_5); | 34 | adc.set_sample_time(SampleTime::Cycles601_5); |
| 30 | 35 | ||
| @@ -34,18 +39,18 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 34 | let mut temperature = adc.enable_temperature(); | 39 | let mut temperature = adc.enable_temperature(); |
| 35 | 40 | ||
| 36 | loop { | 41 | loop { |
| 37 | let vref = adc.read(&mut vrefint); | 42 | let vref = adc.read(&mut vrefint).await; |
| 38 | info!("read vref: {}", vref); | 43 | info!("read vref: {} (should be {})", vref, vrefint.value()); |
| 39 | 44 | ||
| 40 | let temp = adc.read(&mut temperature); | 45 | let temp = adc.read(&mut temperature).await; |
| 41 | info!("read temperature: {}", temp); | 46 | info!("read temperature: {}", temp); |
| 42 | 47 | ||
| 43 | let pin = adc.read(&mut p.PA0); | 48 | let pin = adc.read(&mut p.PA0).await; |
| 44 | info!("read pin: {}", pin); | 49 | info!("read pin: {}", pin); |
| 45 | 50 | ||
| 46 | let pin_mv = pin as u32 * VREF_INT as u32 / vref as u32; | 51 | let pin_mv = (pin as u32 * vrefint.value() as u32 / vref as u32) * 3300 / 4095; |
| 47 | info!("computed pin mv: {}", pin_mv); | 52 | info!("computed pin mv: {}", pin_mv); |
| 48 | 53 | ||
| 49 | Timer::after(Duration::from_secs(1)).await; | 54 | Timer::after(Duration::from_millis(500)).await; |
| 50 | } | 55 | } |
| 51 | } | 56 | } |
