-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathqei.rs
55 lines (40 loc) · 1.11 KB
/
qei.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
//! Testing the Quadrature Encoder Interface
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_semihosting as _;
use cortex_m_semihosting::hprintln;
use cortex_m_rt::entry;
use stm32f1xx_hal::{
pac,
prelude::*,
timer::{pwm_input::QeiOptions, Timer},
};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
// let gpioa = dp.GPIOA.split();
let gpiob = dp.GPIOB.split();
// TIM2
// let c1 = gpioa.pa0;
// let c2 = gpioa.pa1;
// TIM3
// let c1 = gpioa.pa6;
// let c2 = gpioa.pa7;
// TIM4
let c1 = gpiob.pb6;
let c2 = gpiob.pb7;
let qei = Timer::new(dp.TIM4, &clocks).qei((c1, c2), QeiOptions::default());
let mut delay = cp.SYST.delay(&clocks);
loop {
let before = qei.count();
delay.delay_ms(1_000_u16);
let after = qei.count();
let elapsed = after.wrapping_sub(before) as i16;
hprintln!("{}", elapsed);
}
}