qei.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //! Testing the Quadrature Encoder Interface
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_main]
  5. #![no_std]
  6. extern crate panic_semihosting;
  7. use cortex_m_semihosting::hprintln;
  8. use stm32f1xx_hal::{
  9. prelude::*,
  10. pac,
  11. delay::Delay,
  12. qei::Qei,
  13. };
  14. use cortex_m_rt::entry;
  15. #[entry]
  16. fn main() -> ! {
  17. let dp = pac::Peripherals::take().unwrap();
  18. let cp = cortex_m::Peripherals::take().unwrap();
  19. let mut flash = dp.FLASH.constrain();
  20. let mut rcc = dp.RCC.constrain();
  21. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  22. let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
  23. // let gpioa = dp.GPIOA.split(&mut rcc.apb2);
  24. let gpiob = dp.GPIOB.split(&mut rcc.apb2);
  25. // TIM2
  26. // let c1 = gpioa.pa0;
  27. // let c2 = gpioa.pa1;
  28. // TIM3
  29. // let c1 = gpioa.pa6;
  30. // let c2 = gpioa.pa7;
  31. // TIM4
  32. let c1 = gpiob.pb6;
  33. let c2 = gpiob.pb7;
  34. let qei = Qei::tim4(dp.TIM4, (c1, c2), &mut afio.mapr, &mut rcc.apb1);
  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. }