diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/mspm0g3507/src/bin/adc.rs | 39 | ||||
| -rw-r--r-- | examples/mspm0l1306/src/bin/adc.rs | 39 |
2 files changed, 78 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..ceccc7c02 --- /dev/null +++ b/examples/mspm0g3507/src/bin/adc.rs | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_mspm0::adc::{self, Adc, 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 | // Configure adc with sequence 0 to 1 | ||
| 21 | let mut adc = Adc::new_async(p.ADC0, Default::default(), Irqs); | ||
| 22 | let sequence = [(&p.PA22.into(), Vrsel::VddaVssa), (&p.PB20.into(), Vrsel::VddaVssa)]; | ||
| 23 | let mut readings = [0u16; 2]; | ||
| 24 | |||
| 25 | loop { | ||
| 26 | let r = adc.read_channel(&p.PA27).await; | ||
| 27 | info!("Raw adc PA27: {}", r); | ||
| 28 | // With a voltage range of 0-3.3V and a resolution of 12 bits, the raw value can be | ||
| 29 | // approximated to voltage (~0.0008 per step). | ||
| 30 | let mut x = r as u32; | ||
| 31 | x = x * 8; | ||
| 32 | info!("Adc voltage PA27: {},{:#04}", x / 10_000, x % 10_000); | ||
| 33 | // Read a sequence of channels | ||
| 34 | adc.read_sequence(sequence.into_iter(), &mut readings).await; | ||
| 35 | info!("Raw adc sequence: {}", readings); | ||
| 36 | |||
| 37 | Timer::after_millis(400).await; | ||
| 38 | } | ||
| 39 | } | ||
diff --git a/examples/mspm0l1306/src/bin/adc.rs b/examples/mspm0l1306/src/bin/adc.rs new file mode 100644 index 000000000..2806b98cc --- /dev/null +++ b/examples/mspm0l1306/src/bin/adc.rs | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_mspm0::adc::{self, Adc, 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 | // Configure adc with sequence 0 to 1 | ||
| 21 | let mut adc = Adc::new_async(p.ADC0, Default::default(), Irqs); | ||
| 22 | let sequence = [(&p.PA22.into(), Vrsel::VddaVssa), (&p.PA20.into(), Vrsel::VddaVssa)]; | ||
| 23 | let mut readings = [0u16; 2]; | ||
| 24 | |||
| 25 | loop { | ||
| 26 | let r = adc.read_channel(&p.PA27).await; | ||
| 27 | info!("Raw adc PA27: {}", r); | ||
| 28 | // With a voltage range of 0-3.3V and a resolution of 12 bits, the raw value can be | ||
| 29 | // approximated to voltage (~0.0008 per step). | ||
| 30 | let mut x = r as u32; | ||
| 31 | x = x * 8; | ||
| 32 | info!("Adc voltage PA27: {},{:#04}", x / 10_000, x % 10_000); | ||
| 33 | // Read a sequence of channels | ||
| 34 | adc.read_sequence(sequence.into_iter(), &mut readings).await; | ||
| 35 | info!("Raw adc sequence: {}", readings); | ||
| 36 | |||
| 37 | Timer::after_millis(400).await; | ||
| 38 | } | ||
| 39 | } | ||
