定时器——输入捕获

发布于 2024-03-24  91 次阅读


选择TIMx_CH1

定时器配置

uint16_t count, frq;

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)//记不住可以在stm32g4xx_hal_tim.h中最后找到
{
	if(htim->Instance == TIM2)
	{
		count = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);//获取计数
		__HAL_TIM_SetCounter(htim,0);//清零计数
		frq = (80000000/80)/count;//计算频率
		HAL_TIM_IC_Start(htim, TIM_CHANNEL_1);//重新打开定时器
	}
}
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);//在main中调用,开启定时器
最后更新于 2024-03-24