1 基本配置

烧录选择串行:

image-20250209222201608

选择时钟

image-20250209222216679

设置时钟频率

image-20250209222251971

重设系统滴答定时器中断优先级

image-20250209222257565

设置文件名和选择 IDE

image-20250209222305274

设置只拷贝需要的库和创建独立. c 和. h 文件

image-20250209222310379

建立工程并打开后在 Debug >> Settings 中勾选 Reset and Run

2 编码器配置

  • 选择编码器模式,并配置定时器的分频系数和周期

image-20240717001309509

  • 配置编码器模式为 TI 1 和 TI 2 模式,检测边沿为两个 rising

image-20240717001313588

image-20240717001319032

  • 在主函数中开启编码器
1
2
HAL_TIM_Encoder_Start(&htim3,TIM_CHANNEL_ALL);
HAL_TIM_Encoder_Start(&htim4,TIM_CHANNEL_ALL);
  • 编码器读取函数
1
2
3
4
5
6
7
void readEncoder(int speed[]){
speed[0] = -(short)__HAL_TIM_GET_COUNTER(&htim4);
speed[1] = -(short)__HAL_TIM_GET_COUNTER(&htim3);

__HAL_TIM_SET_COUNTER(&htim3, 0);
__HAL_TIM_SET_COUNTER(&htim4, 0);
}