Files
DTU-LCD/Drivers/BSP/TIM/gtim.c
2026-01-24 20:03:14 +08:00

50 lines
1.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
******************************************************************************
* @文件 gtim.c
* @作者 阜阳师范大学物电学院
* @版本 V0.1
* @日期 2025-04-1
* @简介 通用定时器模板程序
****
*/
#include "gtim.h"
#include "key.h"
TIM_HandleTypeDef g_timx_handle = {0}; /* 定时器句柄 */
/**
* @brief 通用定时器 TIMx 定时中断初始化函数
* @note
* 定时器溢出时间计算方法: Tout = ((arr+1) * (psc + 1)) / Ft us
* Ft=晶振工作频率:Mhz
*
* @param arr: 自动重装载值
* @param psc: 时钟预分频数
* @retval 无
*/
void gtim_timx_int_init(uint16_t arr, uint16_t psc)
{
GTIM_TIMX_INT_CLK_ENABLE(); /* 使能 TIMx 时钟 */
g_timx_handle.Instance = GTIM_TIMX_INT; /* 通用定时器x */
g_timx_handle.Init.Prescaler = psc; /* 预分频系数 */
g_timx_handle.Init.CounterMode = TIM_COUNTERMODE_UP; /* 递增计数模式 */
g_timx_handle.Init.Period = arr; /* 自动装载值 */
HAL_TIM_Base_Init(&g_timx_handle);
/*数字越小,优先级越高*/
HAL_NVIC_SetPriority(GTIM_TIMX_INT_IRQn, 2, 0); /* 设置中断优先级抢占优先级2子优先级0 */
HAL_NVIC_EnableIRQ(GTIM_TIMX_INT_IRQn); /* 开启 TIMx 中断 */
HAL_TIM_Base_Start_IT(&g_timx_handle); /* 使能定时器 x 和定时器 x 更新中断 */
}