[STM32] HAL library - use timer

1. Basic timing function

1. Project basic settings (clock tree, RCC selection, SYS)
link:
2. Enter TIMx-Mode-Clock Source and select Internal Clock
insert image description here
3. Timing time calculation formula
Timing time = (Prescaler+1) × (Counter +1) / timing For example, if the timing
time is 1ms, you can set Prescaler = 72-1; Counter = 1000 - 1; (TIM2 clock frequency is set to 72MHz)
4. Enable timer interrupt and generate project

5. Add your code
(1 ) Find the start timer interrupt function in hal_tim.c and add it to the initialization of the main function

  /* USER CODE BEGIN 2 */
	HAL_TIM_Base_Start_IT(&htim2);
  /* USER CODE END 2 */

(2) There is a callback function in hal_tim_ex.c, copy it to the main function, and add your function;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
	{
    
    
		if(htim->Instance == TIM2)
		{
    
    
			//用户代码
		}
}

2. PWM function

2.1. Ordinary timer PWM function

1. Project basic settings (clock tree, RCC selection, SYS)
link:
2. Enter TIMx-Mode-Clock Source and select Internal Clock, select the PWM channel you use
insert image description here
3. Set your PWM frequency according to the calculation formula of timing time,
for example , the timing time is 1ms, you can set Prescaler = 72-1; Counter = 1000 - 1; (TIM2 clock frequency is set to 72MHz), then the PWM frequency is 1KHz.
4. Generate project
5. Add your code
(1) Add timer start function in initialization

  /* USER CODE BEGIN 2 */
	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
  /* USER CODE END 2 */

(2) Set the PWM duty cycle

  while (1)
  {
    
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1,500);

  }

500 is related to the count value you set. If you set the count value to 1000, 500 means the duty cycle is 50%.
insert image description here

2.2, advanced timer PWM function

It is the same as the ordinary timer, but the function name is different in the code initialization and duty cycle setting.
When the basic functions of the advanced timer (TIM1, TIM8) are set,
(1) When Channel1 selects PWM Generation CH1, the function used is the same as the ordinary timer device same

 /* USER CODE BEGIN 2 */
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
  /* USER CODE END 2 */
  while (1)
  {
    
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,500);
  }

(2) When Channel1 selects PWM Generation CH1N, the startup function needs to be changed

  /* USER CODE BEGIN 2 */
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
  /* USER CODE END 2 */

(3) When Channel1 selects PWM Generation CH1N, use the functions in (1) and (2), at this time, two complementary PWMs will be generated,

  /* USER CODE BEGIN 2 */
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
  /* USER CODE END 2 */
 while (1)
  {
    
    
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,300);

  }

insert image description here
(4) More functions
The advanced timer not only has complementary output, but also has braking function and dead time setting, and is mostly used for motor control.

Guess you like

Origin blog.csdn.net/qq_39587650/article/details/119652203