本文主要是介绍STM32CubeMX 配置定时器 HAL库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、 STM32CubeMX配置定时器
二、生成的代码如下
/********************************************************************************* @file tim.c* @brief This file provides code for the configuration* of the TIM instances.******************************************************************************* @attention** <h2><center>© Copyright (c) 2022 STMicroelectronics.* All rights reserved.</center></h2>** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************//* Includes ------------------------------------------------------------------*/
#include "tim.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 */
TIM_HandleTypeDef htim6;
/* TIM6 init function */
void MX_TIM6_Init(void)
{TIM_MasterConfigTypeDef sMasterConfig = {0};htim6.Instance = TIM6;htim6.Init.Prescaler = 71;htim6.Init.CounterMode = TIM_COUNTERMODE_UP;htim6.Init.Period = 1000;htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;if (HAL_TIM_Base_Init(&htim6) != HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK){Error_Handler();}}void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{if(tim_baseHandle->Instance==TIM6){/* USER CODE BEGIN TIM6_MspInit 0 *//* USER CODE END TIM6_MspInit 0 *//* TIM6 clock enable */__HAL_RCC_TIM6_CLK_ENABLE();/* TIM6 interrupt Init */HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);HAL_NVIC_EnableIRQ(TIM6_IRQn);/* USER CODE BEGIN TIM6_MspInit 1 *//* USER CODE END TIM6_MspInit 1 */}
}void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{if(tim_baseHandle->Instance==TIM6){/* USER CODE BEGIN TIM6_MspDeInit 0 *//* USER CODE END TIM6_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_TIM6_CLK_DISABLE();/* TIM6 interrupt Deinit */HAL_NVIC_DisableIRQ(TIM6_IRQn);/* USER CODE BEGIN TIM6_MspDeInit 1 *//* USER CODE END TIM6_MspDeInit 1 */}
}/* USER CODE BEGIN 1 *//* USER CODE END 1 *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
三、定时器的定时周期计算
1、首先我们可以根据下图得知 定时器的定时周期计算公式,但我们不知道TCLK的值
2、打开芯片手册,查看TIM6挂载在哪个时钟树上 ,TIM6挂载在APB1上
3、我们可以调用下面函数获取APB1时钟,我们发现TIM6的时钟并不是APB1的时钟频率,而是它的2倍
uint32_t sysClockFreq = HAL_RCC_GetSysClockFreq(); uint32_t pclk1 = HAL_RCC_GetPCLK1Freq(); uint32_t pclk2 = HAL_RCC_GetPCLK2Freq(); printf("sysClockFreq %d\r\n",sysClockFreq);//系统时钟printf("pclk2 %d\r\n",pclk2);//APB2printf("pclk1 %d\r\n",pclk1);//APB1
4、当我们使用cubemx配置时,我们可以发现APB1的时钟和APB1的外设时钟频率不一致,因此可以得知TIM6的时钟实际上时APB1时钟的2倍
四、开启定时器中断
HAL_TIM_Base_Start_IT(&htim6);//加入定时器中断启动函数
五、编写中断处理函数
方法一:寻找到这个弱定义的函数,在里面编写定时器定时的时间到了以后要执行的动作
/*** @brief Period elapsed callback in non-blocking mode* @param htim TIM handle* @retval None*/
__weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{/* Prevent unused argument(s) compilation warning */// UNUSED(htim);//注释掉这个//然后在这个位置进行代码的编写/* NOTE : This function should not be modified, when the callback is needed,the HAL_TIM_PeriodElapsedCallback could be implemented in the user file*/
}
方法二:重新编写一下TIM6回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim==&htim6){//每次时间计数器到达计数周期就会调用回调函数进行中断累加变量加1 }
}
这篇关于STM32CubeMX 配置定时器 HAL库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!