diff options
| author | Chen Yuheng <[email protected]> | 2024-06-18 17:01:37 +0800 |
|---|---|---|
| committer | Chen Yuheng <[email protected]> | 2024-06-18 17:01:37 +0800 |
| commit | bbe8d3d38a03c66b9c8e2bb70ae067f8ad674059 (patch) | |
| tree | c9e65d2ad2cc302cf0f550f629eb3da9f7a6b9b9 /examples/stm32g0/src/bin/adc.rs | |
| parent | 3c414e99cb3df9a040a92ac413aa3d87faf87dc3 (diff) | |
Add stm32g0 examples
Diffstat (limited to 'examples/stm32g0/src/bin/adc.rs')
| -rw-r--r-- | examples/stm32g0/src/bin/adc.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/adc.rs b/examples/stm32g0/src/bin/adc.rs new file mode 100644 index 000000000..a35119e3d --- /dev/null +++ b/examples/stm32g0/src/bin/adc.rs | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 7 | use embassy_time::Timer; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_stm32::init(Default::default()); | ||
| 13 | info!("Hello World!"); | ||
| 14 | |||
| 15 | let mut adc = Adc::new(p.ADC1); | ||
| 16 | adc.set_sample_time(SampleTime::CYCLES79_5); | ||
| 17 | let mut pin = p.PA1; | ||
| 18 | |||
| 19 | let mut vrefint = adc.enable_vrefint(); | ||
| 20 | let vrefint_sample = adc.read(&mut vrefint); | ||
| 21 | let convert_to_millivolts = |sample| { | ||
| 22 | // From https://www.st.com/resource/en/datasheet/stm32g031g8.pdf | ||
| 23 | // 6.3.3 Embedded internal reference voltage | ||
| 24 | const VREFINT_MV: u32 = 1212; // mV | ||
| 25 | |||
| 26 | (u32::from(sample) * VREFINT_MV / u32::from(vrefint_sample)) as u16 | ||
| 27 | }; | ||
| 28 | |||
| 29 | loop { | ||
| 30 | let v = adc.read(&mut pin); | ||
| 31 | info!("--> {} - {} mV", v, convert_to_millivolts(v)); | ||
| 32 | Timer::after_millis(100).await; | ||
| 33 | } | ||
| 34 | } | ||
