aboutsummaryrefslogtreecommitdiff
path: root/examples/mspm0g3507/src/bin/adc.rs
blob: cf1abb471c608cafe647f476ab3b8b09a53bb968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_mspm0::adc::{self, Adc, Vrsel};
use embassy_mspm0::{Config, bind_interrupts, peripherals};
use embassy_time::Timer;
use {defmt_rtt as _, panic_halt as _};

bind_interrupts!(struct Irqs {
    ADC0 => adc::InterruptHandler<peripherals::ADC0>;
});

#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
    info!("Hello world!");
    let p = embassy_mspm0::init(Config::default());

    // Configure adc with sequence 0 to 1
    let mut adc = Adc::new_async(p.ADC0, Default::default(), Irqs);
    let sequence = [(&p.PA22.into(), Vrsel::VddaVssa), (&p.PB20.into(), Vrsel::VddaVssa)];
    let mut readings = [0u16; 2];

    loop {
        let r = adc.read_channel(&p.PA27).await;
        info!("Raw adc PA27: {}", r);
        // With a voltage range of 0-3.3V and a resolution of 12 bits, the raw value can be
        // approximated to voltage (~0.0008 per step).
        let mut x = r as u32;
        x = x * 8;
        info!("Adc voltage PA27: {},{:#04}", x / 10_000, x % 10_000);
        // Read a sequence of channels
        adc.read_sequence(sequence.into_iter(), &mut readings).await;
        info!("Raw adc sequence: {}", readings);

        Timer::after_millis(400).await;
    }
}