qei.rs 1.3 KB

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