Files
CHJ/user/MCU/lhl_btim.c
2026-03-20 21:19:53 +08:00

151 lines
5.5 KiB
C
Raw Permalink 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.

#include "../main/SystemInclude.h"
/*==================================================================================*/
/*BTimer0 */
/*==================================================================================*/
/**------------------------------------------------------------------------
* @brief 基础定时器 BTIM0 初始化(不启动)
* @note 时钟源为系统时钟 HCLK根据 SystemClockConfiguration 配置变化,如 4MHz/8MHz/16MHz/32MHz
* 定时周期 T = (btim_period + 1) / HCLK单位为秒。
* 配置后使能中断,但不启动计数。
* @param btim_period: 自动重装载值0~65535决定定时周期
* @example
* // 假设 HCLK = 4MHz需要 1ms 周期btim_period = 4e6 * 0.001 - 1 = 3999
* BTIM0_Init(3999);
**/
void BTIM0_Init(uint16_t btim_period)
{
BTIM_Base_InitTypeDef BTIM_InitStructure;
BTIM_InitStructure.MDIS = ENABLE; // 启用基础定时器
BTIM_InitStructure.FreezingMode = DISABLE;
BTIM_InitStructure.ChainedMode = DISABLE;
LHL_BTIM_Base_Init(BTIM, &BTIM_InitStructure);
LHL_BTIM_SetPeriod(BTIMER_0,btim_period );
LHL_BTIM_ITConfig(BTIMER_0, ENABLE);
// NVIC_EnableIRQ(TIM5_IRQn); // BTIMER0对应TIM5_IRQ
}
/**------------------------------------------------------------------------
* @brief 启动 BTIM0 定时器
* @note 调用 BTIM0_Init 完成初始化后立即启动计数。
* @param btim_period: 自动重装载值
* @example
* // HCLK = 4MHz启动 1ms 周期定时器
* StartBtimer0(3999);
**/
void StartBtimer0(uint16_t btim_period)
{
BTIM0_Init(btim_period);
BTIM->TCTRL_0 |= BTIM_TCTRL_TEN_Msk; //开启定时器 LHL_BTIM_Start(BTIMER_0,btim_period,0);
// NVIC_EnableIRQ(TIM5_IRQn); // BTIMER0对应TIM5_IRQ
}
/**------------------------------------------------------------------------
* @brief 停止 BTIM0 定时器
* @note 清除使能位 TEN停止计数。
* @param 无
* @example StopBtimer0();
**/
void StopBtimer0(void)
{
// LHL_BTIM_Stop(BTIMER_0);
BTIM->TCTRL_0 &= ~BTIM_TCTRL_TEN_Msk;
}
/*==================================================================================*/
/*BTimer1 */
/*==================================================================================*/
/**------------------------------------------------------------------------
* @brief 基础定时器 BTIM1 初始化(不启动)
* @note 同 BTIM0时钟源为系统时钟 HCLK。
* @param btim_period: 自动重装载值
* @example BTIM1_Init(3999); // HCLK=4MHz1ms 周期
**/
void BTIM1_Init(uint16_t btim_period)
{
BTIM_Base_InitTypeDef BTIM_InitStructure;
BTIM_InitStructure.MDIS = ENABLE; // 启用基础定时器
BTIM_InitStructure.FreezingMode = DISABLE;
BTIM_InitStructure.ChainedMode = DISABLE;
LHL_BTIM_Base_Init(BTIM, &BTIM_InitStructure);
LHL_BTIM_SetPeriod(BTIMER_1,btim_period );
LHL_BTIM_ITConfig(BTIMER_1, ENABLE);
// NVIC_EnableIRQ(TIM6_IRQn); // BTIMER1对应TIM6_IRQ
}
/**------------------------------------------------------------------------
* @brief 启动 BTIM1 定时器
* @param btim_period: 自动重装载值
* @example StartBtimer1(3999);
**/
void StartBtimer1(uint16_t btim_period)
{
BTIM1_Init(btim_period);
BTIM->TCTRL_1 |= BTIM_TCTRL_TEN_Msk; //开启定时器 LHL_BTIM_Start(BTIMER_0,btim_period,0);
// NVIC_EnableIRQ(TIM6_IRQn); // BTIMER1对应TIM6_IRQ
}
/**------------------------------------------------------------------------
* @brief 停止 BTIM1 定时器
* @note 清除使能位 TEN停止计数。
* @param 无
* @example StopBtimer1();
**/
void StopBtimer1(void)
{
// LHL_BTIM_Stop(BTIMER_1);
BTIM->TCTRL_1 &= ~BTIM_TCTRL_TEN_Msk;
}
/*-=====================================================================================================
//定时器中断回调
-=====================================================================================================*/
static btimer_irq_callback_t btim0_irq_callback ,btim1_irq_callback ;
/**------------------------------------------------------------------------
* @brief 注册 BTIM0/BTIM1 中断回调函数
* @param timer: BTIMER_0 或 BTIMER_1
* @param btim_irq_callback: 用户回调函数指针(无参数无返回值)
* @note 注册时自动清除中断标志并开启对应 NVIC 中断。
* @example Btimer_register_irq_callback(BTIMER_0, my_btim_callback);
**/
void Btimer_register_irq_callback(BTIM_ID_t timer, btimer_irq_callback_t btim_irq_callback)
{
if(timer == BTIMER_0) {btim0_irq_callback = btim_irq_callback; LHL_BTIM_ClearPending(BTIMER_0);NVIC_EnableIRQ(TIM5_IRQn);}
else if(timer == BTIMER_1) {btim1_irq_callback = btim_irq_callback; LHL_BTIM_ClearPending(BTIMER_1);NVIC_EnableIRQ(TIM6_IRQn);}
}
/**------------------------------------------------------------------------
* @brief BTIM0 中断服务函数(对应 TIM5_IRQn
* @note 调用已注册的回调函数(若存在),然后停止定时器并清除中断标志。
**/
void TIM5_IRQHandler(void)
{
if (btim0_irq_callback != NULL) btim0_irq_callback();// 调用用户注册的回调函数
LHL_BTIM_Stop(BTIMER_0); // 停止BTIM
LHL_BTIM_ClearPending(BTIMER_0); // 清中断标志
}
/**------------------------------------------------------------------------
* @brief BTIM1 中断服务函数(对应 TIM6_IRQn
* @note 调用已注册的回调函数(若存在),然后停止定时器并清除中断标志。
**/
void TIM6_IRQHandler(void)
{
if (btim1_irq_callback != NULL) btim1_irq_callback();// 调用用户注册的回调函数
LHL_BTIM_Stop(BTIMER_1); // 停止BTIM
LHL_BTIM_ClearPending(BTIMER_1); // 清中断标志
}