#include "encoder.h" #include static int Direction; static int CaptureNumber; void encoder_start(void) { /* 不开启会导致程序无法启动,原因位置 */ HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_1 | TIM_CHANNEL_2); } void encoder_read_printf(void) { Direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim4); // 读取电机转动方向 CaptureNumber = (short)__HAL_TIM_GET_COUNTER(&htim4); // 读取编码器数据 __HAL_TIM_GET_COUNTER(&htim4) = 0; // 计数器值重新置位 if (CaptureNumber != 0) { printf("Direction is %d,CaptureNumber is %d\r\n", Direction, CaptureNumber); } }