diff options
| author | crispaudio <[email protected]> | 2025-09-08 09:10:16 +0200 |
|---|---|---|
| committer | crispaudio <[email protected]> | 2025-09-08 09:10:16 +0200 |
| commit | bbcaab13bc074d8223b43d8e05682b708c192d78 (patch) | |
| tree | 84bbe6af4b28f855a2c57d47413ec3cff44f6671 /examples/mspm0l1306 | |
| parent | a6cd24907aa43a8178a16b0db3d6b376f67f7540 (diff) | |
mspm0-adc: add adc with examples
Diffstat (limited to 'examples/mspm0l1306')
| -rw-r--r-- | examples/mspm0l1306/src/bin/adc.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/mspm0l1306/src/bin/adc.rs b/examples/mspm0l1306/src/bin/adc.rs new file mode 100644 index 000000000..9ede31fed --- /dev/null +++ b/examples/mspm0l1306/src/bin/adc.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_mspm0::adc::{self, Adc, AdcChannel, AdcConfig, Resolution, Vrsel}; | ||
| 7 | use embassy_mspm0::{bind_interrupts, peripherals, Config}; | ||
| 8 | use embassy_time::Timer; | ||
| 9 | use {defmt_rtt as _, panic_halt as _}; | ||
| 10 | |||
| 11 | bind_interrupts!(struct Irqs { | ||
| 12 | ADC0 => adc::InterruptHandler<peripherals::ADC0>; | ||
| 13 | }); | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) -> ! { | ||
| 17 | info!("Hello world!"); | ||
| 18 | let p = embassy_mspm0::init(Config::default()); | ||
| 19 | |||
| 20 | let adc_config = AdcConfig { | ||
| 21 | resolution: Resolution::BIT12, | ||
| 22 | vr_select: Vrsel::VddaVssa, | ||
| 23 | sample_time: 50, | ||
| 24 | }; | ||
| 25 | |||
| 26 | // Configure adc with sequence 0 to 1 | ||
| 27 | let mut adc = Adc::new_async(p.ADC0, adc_config, Irqs); | ||
| 28 | let pin1 = p.PA22.degrade_adc(); | ||
| 29 | let pin2 = p.PA20.degrade_adc(); | ||
| 30 | let sequence = [(&pin1, Vrsel::VddaVssa), (&pin2, Vrsel::VddaVssa)]; | ||
| 31 | let mut readings = [0u16; 2]; | ||
| 32 | let mut pin3 = p.PA27; | ||
| 33 | |||
| 34 | loop { | ||
| 35 | let r = adc.read_channel(&mut pin3).await; | ||
| 36 | info!("Raw adc PA27: {}", r); | ||
| 37 | // With a voltage range of 0-3.3V and a resolution of 12 bits, the raw value can be | ||
| 38 | // approximated to voltage (~0.0008 per step). | ||
| 39 | let mut x = r as u32; | ||
| 40 | x = x * 8; | ||
| 41 | info!("Adc voltage PA27: {},{:#04}", x / 10_000, x % 10_000); | ||
| 42 | // Read a sequence of channels | ||
| 43 | adc.read_sequence(sequence.into_iter(), &mut readings).await; | ||
| 44 | info!("Raw adc sequence: {}", readings); | ||
| 45 | |||
| 46 | Timer::after_millis(400).await; | ||
| 47 | } | ||
| 48 | } | ||
