Skip to content

Commit 476eb40

Browse files
authored
Merge pull request #13 from HarkonenBade/adc-test
Initial implementation of ADC interface
2 parents 00c46e1 + 0fa7a20 commit 476eb40

File tree

6 files changed

+478
-1
lines changed

6 files changed

+478
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
- Added Sync & Send ability to Pin
1313
- Added overflow guards to delay
14+
- Added initial implementation of an ADC interface (#13) - @HarkonenBade
1415

1516
## [v0.10.0] - 2018-12-23
1617

examples/blinky_adc.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
#[allow(unused_imports)]
5+
use panic_halt;
6+
7+
use stm32f0xx_hal as hal;
8+
9+
use crate::hal::delay::Delay;
10+
use crate::hal::prelude::*;
11+
use crate::hal::stm32;
12+
13+
use crate::hal::adc::Adc;
14+
15+
use cortex_m::peripheral::Peripherals;
16+
use cortex_m_rt::entry;
17+
18+
#[entry]
19+
fn main() -> ! {
20+
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
21+
let gpioa = p.GPIOA.split();
22+
23+
/* (Re-)configure PA1 as output */
24+
let mut led = gpioa.pa1.into_push_pull_output();
25+
26+
/* (Re-)configure PA0 as analog in */
27+
let mut an_in = gpioa.pa0.into_analog();
28+
29+
/* Constrain clocking registers */
30+
let rcc = p.RCC.constrain();
31+
32+
/* Configure clock to 8 MHz (i.e. the default) and freeze it */
33+
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
34+
35+
/* Get delay provider */
36+
let mut delay = Delay::new(cp.SYST, clocks);
37+
38+
let mut adc = Adc::new(p.ADC);
39+
40+
loop {
41+
led.toggle();
42+
43+
let val: u16 = adc.read(&mut an_in).unwrap();
44+
45+
/* shift the value right by 3, same as divide by 8, reduces
46+
the 0-4095 range into something approximating 1-512 */
47+
let time: u16 = (val >> 3) + 1;
48+
49+
delay.delay_ms(time);
50+
}
51+
}
52+
53+
loop {
54+
continue;
55+
}
56+
}

0 commit comments

Comments
 (0)