-
Notifications
You must be signed in to change notification settings - Fork 221
/
uno-adc.rs
58 lines (48 loc) · 1.94 KB
/
uno-adc.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![no_std]
#![no_main]
use arduino_hal::prelude::*;
use panic_halt as _;
use arduino_hal::adc;
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);
let mut adc = arduino_hal::Adc::new(dp.ADC, Default::default());
let (vbg, gnd, tmp) = (
adc.read_blocking(&adc::channel::Vbg),
adc.read_blocking(&adc::channel::Gnd),
adc.read_blocking(&adc::channel::Temperature),
);
ufmt::uwriteln!(&mut serial, "Vbandgap: {}", vbg).void_unwrap();
ufmt::uwriteln!(&mut serial, "Ground: {}", gnd).void_unwrap();
ufmt::uwriteln!(&mut serial, "Temperature: {}", tmp).void_unwrap();
let a0 = pins.a0.into_analog_input(&mut adc);
let a1 = pins.a1.into_analog_input(&mut adc);
let a2 = pins.a2.into_analog_input(&mut adc);
let a3 = pins.a3.into_analog_input(&mut adc);
let a4 = pins.a4.into_analog_input(&mut adc);
let a5 = pins.a5.into_analog_input(&mut adc);
loop {
let values = [
a0.analog_read(&mut adc),
a1.analog_read(&mut adc),
a2.analog_read(&mut adc),
a3.analog_read(&mut adc),
a4.analog_read(&mut adc),
a5.analog_read(&mut adc),
];
for (i, v) in values.iter().enumerate() {
ufmt::uwrite!(&mut serial, "A{}: {} ", i, v).void_unwrap();
}
// Arduino Nano has two more ADC pins A6 and A7. Accessing them works a bit different from
// the other pins as they are not normal IO pins. The code below shows how it works.
let (a6, a7) = (
adc.read_blocking(&adc::channel::ADC6),
adc.read_blocking(&adc::channel::ADC7),
);
ufmt::uwrite!(&mut serial, "A6: {} A7: {}", a6, a7).void_unwrap();
ufmt::uwriteln!(&mut serial, "").void_unwrap();
arduino_hal::delay_ms(1000);
}
}