[STM32] HAL library PWM control motor speed and encoder reading (super detailed)

  Hello everyone, I am Xiaozheng. In this article, I will give a detailed explanation of the STM32CubeMX configuration process for PWM control motors and encoders to read motor speeds, so that friends who are going to learn the HAL library can better understand how STM32CubeMX is configured.

If you have a small partner who does not understand the motor drive and encoder, please see:
Motor drive explanation : [Balance car production] (2) Motor drive (super detailed explanation)
encoder explanation : [Balance car production] (3) Encoder explanation (super Detailed)

  The next article will introduce the use of PID algorithm closed-loop control of motor speed (HAL library) .

  Not much to say, let's start learning today!

1. Hardware preparation

(1) Required hardware

  • Chip: STM32F103RCT6
  • Drive: motor drive board or TB6612 (motor drive chip)
  • Battery: 12V Libo battery
  • Motor: motor with encoder
  • Wheels: I use mecanum wheels

(2) Hardware connection:

  • PA8 —— Motor drive board PWM1
  • GND —— Motor drive board PWM2
    (here, if you want to reverse, then PWM1 is grounded, and PWM2 is connected to PA8; if both of them need to be connected to the PWM pin in the car, forward rotation: PWM1 gives a certain duty cycle, PWM2 duty cycle is 0 )
  • PA9(USART1_RX) —— connect to serial port TX
  • PA10(USART1_TX) —— connect to serial port RX
  • PB6 —— Encoder A
  • PB7 —— Encoder B

2. STM32CubeMX configuration:

1.1 Tools used:

  • Chip: STM32F103RCT6
  • IDE: MDK-Keil software
  • STM32F1xxHAL library

1.2 Knowledge summary:

  • STM32CubeMX creates TIMx, USART routines
  • Keil software programming

1.3 Project creation
1. Chip selection
  chip: STM32F103RCT6 ( select according to your own board)

Insert picture description here
2. Set RCC,
  set high-speed external clock HSE, select external clock source

Insert picture description here
3. LED1 configuration
  The LED1 pin of the board I use is PD2, and the initial level is high. The purpose is to judge whether the timer interrupt is entered by observing the light on and off

Insert picture description here
4. USART1 configures
  asynchronous transmission and reception, the default baud rate is 115200 Bit/s

Insert picture description here
5. PWM configuration
  uses timer 1 channel 1 and channel 4 (TIM1_CH1 and TIM1_CH4), the frequency is 10KHz

Insert picture description here
6. The timer interrupt configuration
  uses timer 2 (TIM2), the period is set to 10ms, that is, the timer interrupt is entered once in 10ms

Insert picture description here
  Turn on update timer interrupt

Insert picture description here
7. Encoder configuration
  STM32 comes with encoder configuration, use timer 4 (TIM4_CH1 and TIM4_CH2), open the update timer interrupt

Insert picture description here
8. Interrupt priority configuration
  Because the encoder interrupt will occur within the timing 10ms interrupt, the preemption priority of the encoder interrupt should be greater than the timing 10ms

Insert picture description here
9. Configuration clock
  F1 series chip system clock is 72MHzs

Insert picture description here
10. The final steps of project creation

  • Set project name
  • Choose IDE

Insert picture description here
11. Output file

  • ②Place: Copy the .c and .h of the files used
  • ③ Office: Each function produces independent .c and .h files

Insert picture description here
12. Create a project file and
  click GENERATE CODE to create a project

13. Configure the download tool.
  Here we need to check the download and run it directly, and then perform a compilation

Insert picture description here


3. STM32 source code:

(1) The following code should be easy to understand for those who have used the hal library before. Xiaozheng will not explain too much. If you don't understand, please ask questions under the comment area. Add in main.c:

/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "math.h"
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
unsigned int MotorSpeed;  // 电机当前速度数值,从编码器中获取
int MotorOutput;		  // 电机输出
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
int fputc(int ch, FILE *p)
{
    
    
	while(!(USART1->SR & (1<<7)));
	USART1->DR = ch;
	
	return ch;
}
/* USER CODE END 0 */
  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);	    // TIM1_CH1(pwm)
  HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_1); // 开启编码器A
  HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_2); // 开启编码器B	
  HAL_TIM_Base_Start_IT(&htim2);                // 使能定时器2中断
  /* USER CODE END 2 */
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    
    
    static unsigned char i = 0;
    if (htim == (&htim2))
    {
    
    
        //1.获取电机速度
        MotorSpeed = (short)(__HAL_TIM_GET_COUNTER(&htim4)/18);   
        // TIM4计数器获得电机脉冲,该电机在10ms采样的脉冲/18则为实际转速的rpm
        __HAL_TIM_SET_COUNTER(&htim4,0);  // 计数器清零
        
      
        //2.将占空比导入至电机控制函数
        MotorOutput=3600; // 3600即为50%的占空比
        __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, MotorOutput);
        MotorOutput=MotorOutput*100/7200; 
        // 占空比(按最高100%计算,串口打印)
        i++;
        if(i>100)
        {
    
    
          // 通过观察小灯亮灭来判断是否正确进入定时器中断
          HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_2);
          // 打印定时器4的计数值,short(-32768——32767)
          printf("Encoder = %d moto = %d \r\n",MotorSpeed,MotorOutput);	
          i=0;
        }
    }
}
/* USER CODE END 4 */

(2) Burn into STM32, serial port display:

Insert picture description here
(3) Physical display:

Insert picture description here

4. Download

(1) Program download address: https://pan.baidu.com/s/1mXYS8lQKaMX6XNA961UuJw
Extraction code: z3ax

(2) Serial port assistant download address: https://pan.baidu.com/s/11xBkoLBMVcIv7QNALpOeeg
extraction code: yzx3

5. Summary

  The above is the HAL library configuration and keil programming for the PWM control of the motor speed and the encoder to read the motor speed, and the use of PID algorithm closed-loop control of the motor speed (HAL library) will be introduced later . If there is an error in the article or your friends have questions about the above content , Welcome everyone to leave a message in the comment area, Xiaozheng will reply to you as soon as possible after seeing it! See you next time!

Guess you like

Origin blog.csdn.net/weixin_44270218/article/details/114045057