diff options
| author | Chen Yuheng <[email protected]> | 2024-07-11 10:33:43 +0800 |
|---|---|---|
| committer | Chen Yuheng <[email protected]> | 2024-07-11 10:33:43 +0800 |
| commit | f01ffbcc12c40c68a57bc2daffdfad697bc28921 (patch) | |
| tree | ca32b8dd65d7abeb050eebdf72e4b13b765c2438 /examples/stm32g4/src/bin/adc_differential.rs | |
| parent | 6636a5835b27f82abea9000c9f3e93b4455b29c8 (diff) | |
Add oversampling and differential for g4
Diffstat (limited to 'examples/stm32g4/src/bin/adc_differential.rs')
| -rw-r--r-- | examples/stm32g4/src/bin/adc_differential.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/stm32g4/src/bin/adc_differential.rs b/examples/stm32g4/src/bin/adc_differential.rs new file mode 100644 index 000000000..78d071d45 --- /dev/null +++ b/examples/stm32g4/src/bin/adc_differential.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | //! adc differential mode example | ||
| 2 | //! | ||
| 3 | //! This example uses adc1 in differential mode | ||
| 4 | //! p:pa0 n:pa1 | ||
| 5 | |||
| 6 | #![no_std] | ||
| 7 | #![no_main] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 12 | use embassy_stm32::Config; | ||
| 13 | use embassy_time::Timer; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | { | ||
| 20 | use embassy_stm32::rcc::*; | ||
| 21 | config.rcc.pll = Some(Pll { | ||
| 22 | source: PllSource::HSI, | ||
| 23 | prediv: PllPreDiv::DIV4, | ||
| 24 | mul: PllMul::MUL85, | ||
| 25 | divp: None, | ||
| 26 | divq: None, | ||
| 27 | // Main system clock at 170 MHz | ||
| 28 | divr: Some(PllRDiv::DIV2), | ||
| 29 | }); | ||
| 30 | config.rcc.mux.adc12sel = mux::Adcsel::SYS; | ||
| 31 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 32 | } | ||
| 33 | let mut p = embassy_stm32::init(config); | ||
| 34 | |||
| 35 | let mut adc = Adc::new(p.ADC1); | ||
| 36 | adc.set_sample_time(SampleTime::CYCLES247_5); | ||
| 37 | adc.set_differential(&mut p.PA0, true); //p:pa0,n:pa1 | ||
| 38 | |||
| 39 | // can also use | ||
| 40 | // adc.set_differential_channel(1, true); | ||
| 41 | info!("adc initialized"); | ||
| 42 | loop { | ||
| 43 | let measured = adc.blocking_read(&mut p.PA0); | ||
| 44 | info!("data: {}", measured); | ||
| 45 | Timer::after_millis(500).await; | ||
| 46 | } | ||
| 47 | } | ||
