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.

64 lines
1.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "encoder.h"
  2. #include "zboard.h"
  3. #include <stdio.h>
  4. static int Direction;
  5. static int CaptureNumber;
  6. static encoder_t m_uarts[] = {
  7. {&camera_encoder, TIM_CHANNEL_1 | TIM_CHANNEL_2}, // 相机编码器
  8. {&driven_encoder_gear, TIM_CHANNEL_1 | TIM_CHANNEL_2} // 从动编码器
  9. };
  10. void encoder_all_start(void)
  11. {
  12. /* 不开启会导致程序无法启动,原因未知 */
  13. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  14. {
  15. HAL_TIM_Encoder_Start(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
  16. }
  17. }
  18. void encoder_all_stop(void)
  19. {
  20. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  21. {
  22. HAL_TIM_Encoder_Stop(m_uarts[i].tim_handler, m_uarts[i].tim_channel);
  23. }
  24. }
  25. bool encoder_clear_counter(TIM_HandleTypeDef *tim_handler)
  26. {
  27. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  28. {
  29. if (tim_handler == m_uarts[i].tim_handler)
  30. {
  31. __HAL_TIM_GET_COUNTER(tim_handler) = 0;
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. void encoder_all_clear_counter(void)
  38. {
  39. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  40. {
  41. __HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0; // 计数器值重新置位
  42. }
  43. }
  44. void encoder_read_printf(void)
  45. {
  46. for (uint8_t i = 0; i < (sizeof(m_uarts) / sizeof(encoder_t)); i++)
  47. {
  48. Direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(m_uarts[i].tim_handler); // 读取电机转动方向
  49. CaptureNumber = (short)__HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler); // 读取编码器数据
  50. __HAL_TIM_GET_COUNTER(m_uarts[i].tim_handler) = 0; // 计数器值重新置位
  51. if (CaptureNumber != 0)
  52. {
  53. printf("Encoder number:%d,Direction is %d,CaptureNumber is %d\r\n", i, Direction, CaptureNumber);
  54. }
  55. }
  56. }