You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
664 B

  1. #include "encoder.h"
  2. #include <stdio.h>
  3. static int Direction;
  4. static int CaptureNumber;
  5. void encoder_start(void)
  6. {
  7. /* 不开启会导致程序无法启动,原因位置 */
  8. HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_1 | TIM_CHANNEL_2);
  9. }
  10. void encoder_read_printf(void)
  11. {
  12. Direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim4); // 读取电机转动方向
  13. CaptureNumber = (short)__HAL_TIM_GET_COUNTER(&htim4); // 读取编码器数据
  14. __HAL_TIM_GET_COUNTER(&htim4) = 0; // 计数器值重新置位
  15. if (CaptureNumber != 0)
  16. {
  17. printf("Direction is %d,CaptureNumber is %d\r\n", Direction, CaptureNumber);
  18. }
  19. }