qei.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //! Testing the Quadrature Encoder Interface
  2. #![deny(unsafe_code)]
  3. #![no_main]
  4. #![no_std]
  5. use panic_semihosting as _;
  6. use cortex_m_semihosting::hprintln;
  7. use stm32f1xx_hal::{
  8. prelude::*,
  9. pac,
  10. delay::Delay,
  11. timer::Timer,
  12. };
  13. use cortex_m_rt::entry;
  14. #[entry]
  15. fn main() -> ! {
  16. let dp = pac::Peripherals::take().unwrap();
  17. let cp = cortex_m::Peripherals::take().unwrap();
  18. let mut flash = dp.FLASH.constrain();
  19. let mut rcc = dp.RCC.constrain();
  20. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  21. let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
  22. // let gpioa = dp.GPIOA.split(&mut rcc.apb2);
  23. let gpiob = dp.GPIOB.split(&mut rcc.apb2);
  24. // TIM2
  25. // let c1 = gpioa.pa0;
  26. // let c2 = gpioa.pa1;
  27. // TIM3
  28. // let c1 = gpioa.pa6;
  29. // let c2 = gpioa.pa7;
  30. // TIM4
  31. let c1 = gpiob.pb6;
  32. let c2 = gpiob.pb7;
  33. let qei = Timer::tim4(dp.TIM4, &clocks, &mut rcc.apb1)
  34. .qei((c1, c2), &mut afio.mapr);
  35. let mut delay = Delay::new(cp.SYST, clocks);
  36. loop {
  37. let before = qei.count();
  38. delay.delay_ms(1_000_u16);
  39. let after = qei.count();
  40. let elapsed = after.wrapping_sub(before) as i16;
  41. hprintln!("{}", elapsed).unwrap();
  42. }
  43. }