aboutsummaryrefslogtreecommitdiff
path: root/examples/mspm0g3507/src
diff options
context:
space:
mode:
authorcrispaudio <[email protected]>2025-09-08 09:10:16 +0200
committercrispaudio <[email protected]>2025-09-08 09:10:16 +0200
commitbbcaab13bc074d8223b43d8e05682b708c192d78 (patch)
tree84bbe6af4b28f855a2c57d47413ec3cff44f6671 /examples/mspm0g3507/src
parenta6cd24907aa43a8178a16b0db3d6b376f67f7540 (diff)
mspm0-adc: add adc with examples
Diffstat (limited to 'examples/mspm0g3507/src')
-rw-r--r--examples/mspm0g3507/src/bin/adc.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/mspm0g3507/src/bin/adc.rs b/examples/mspm0g3507/src/bin/adc.rs
new file mode 100644
index 000000000..fed8b9dd3
--- /dev/null
+++ b/examples/mspm0g3507/src/bin/adc.rs
@@ -0,0 +1,48 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_mspm0::adc::{self, Adc, AdcChannel, AdcConfig, Resolution, Vrsel};
7use embassy_mspm0::{bind_interrupts, peripherals, Config};
8use embassy_time::Timer;
9use {defmt_rtt as _, panic_halt as _};
10
11bind_interrupts!(struct Irqs {
12 ADC0 => adc::InterruptHandler<peripherals::ADC0>;
13});
14
15#[embassy_executor::main]
16async 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.PB20.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}