-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathadc.rs
52 lines (40 loc) · 1.51 KB
/
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
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_semihosting as _;
use cortex_m_rt::entry;
use stm32f1xx_hal::{adc, pac, prelude::*};
use cortex_m_semihosting::hprintln;
#[entry]
fn main() -> ! {
// Acquire peripherals
let p = pac::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain();
let rcc = p.RCC.constrain();
// Configure ADC clocks
// Default value is the slowest possible ADC clock: PCLK2 / 8. Meanwhile ADC
// clock is configurable. So its frequency may be tweaked to meet certain
// practical needs. User specified value is be approximated using supported
// prescaler values 2/4/6/8.
let clocks = rcc.cfgr.adcclk(2.MHz()).freeze(&mut flash.acr);
hprintln!("adc freq: {}", clocks.adcclk());
// Setup ADC
let mut adc1 = adc::Adc::adc1(p.ADC1, &clocks);
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
let mut adc2 = adc::Adc::adc2(p.ADC2, &clocks);
// Setup GPIOB
let mut gpiob = p.GPIOB.split();
// Configure pb0, pb1 as an analog input
let mut ch0 = gpiob.pb0.into_analog(&mut gpiob.crl);
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
let mut ch1 = gpiob.pb1.into_analog(&mut gpiob.crl);
loop {
let data: u16 = adc1.read(&mut ch0).unwrap();
hprintln!("adc1: {}", data);
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
{
let data1: u16 = adc2.read(&mut ch1).unwrap();
hprintln!("adc2: {}", data1);
}
}
}