diff options
| author | Chen Yuheng <[email protected]> | 2024-06-27 17:04:26 +0800 |
|---|---|---|
| committer | Chen Yuheng <[email protected]> | 2024-06-27 17:04:26 +0800 |
| commit | a0799bf270010f4e91b0c3eebe487d8bc6fb54fc (patch) | |
| tree | 23e2090e0e53118a71e3d57417503104cc07e1cc /examples/stm32g0/src | |
| parent | a2acb3e3dceddb4752f8fb1c17aa65e1959a2180 (diff) | |
Add adc oversampling support
Diffstat (limited to 'examples/stm32g0/src')
| -rw-r--r-- | examples/stm32g0/src/bin/adc_oversampling.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/adc_oversampling.rs b/examples/stm32g0/src/bin/adc_oversampling.rs new file mode 100644 index 000000000..3c31eb206 --- /dev/null +++ b/examples/stm32g0/src/bin/adc_oversampling.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | //! adc oversampling example | ||
| 2 | //! | ||
| 3 | //! This example uses adc oversampling to achieve 16bit data | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 11 | use embassy_time::Timer; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: Spawner) { | ||
| 16 | let p = embassy_stm32::init(Default::default()); | ||
| 17 | info!("Adc oversample test"); | ||
| 18 | |||
| 19 | let mut adc = Adc::new(p.ADC1); | ||
| 20 | adc.set_sample_time(SampleTime::CYCLES1_5); | ||
| 21 | let mut pin = p.PA1; | ||
| 22 | |||
| 23 | // From https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf | ||
| 24 | // page373 15.8 Oversampler | ||
| 25 | // Table 76. Maximum output results vs N and M. Grayed values indicates truncation | ||
| 26 | // 0x00 oversampling ratio X2 | ||
| 27 | // 0x01 oversampling ratio X4 | ||
| 28 | // 0x02 oversampling ratio X8 | ||
| 29 | // 0x03 oversampling ratio X16 | ||
| 30 | // 0x04 oversampling ratio X32 | ||
| 31 | // 0x05 oversampling ratio X64 | ||
| 32 | // 0x06 oversampling ratio X128 | ||
| 33 | // 0x07 oversampling ratio X256 | ||
| 34 | adc.set_oversampling_ratio(0x03); | ||
| 35 | adc.set_oversampling_shift(0b0000); | ||
| 36 | adc.oversampling_enable(true); | ||
| 37 | |||
| 38 | loop { | ||
| 39 | let v = adc.read(&mut pin); | ||
| 40 | info!("--> {} ", v); //max 65520 = 0xFFF0 | ||
| 41 | Timer::after_millis(100).await; | ||
| 42 | } | ||
| 43 | } | ||
