-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathadc.rs
More file actions
54 lines (41 loc) · 1.53 KB
/
adc.rs
File metadata and controls
54 lines (41 loc) · 1.53 KB
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
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_semihosting as _;
use cortex_m_rt::entry;
use stm32f1xx_hal::{adc, pac, prelude::*, rcc};
use cortex_m_semihosting::hprintln;
#[entry]
fn main() -> ! {
// Acquire peripherals
let p = pac::Peripherals::take().unwrap();
let mut flash = p.FLASH.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 mut rcc = p
.RCC
.freeze(rcc::Config::hsi().adcclk(2.MHz()), &mut flash.acr);
hprintln!("adc freq: {}", rcc.clocks.adcclk());
// Setup ADC
let mut adc1 = adc::Adc::new(p.ADC1, &mut rcc);
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
let mut adc2 = adc::Adc::new(p.ADC2, &mut rcc);
// Setup GPIOB
let mut gpiob = p.GPIOB.split(&mut rcc);
// 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);
}
}
}