此处我们使用cubeide完成代码初始化
打开定时器8四路捕获
配置引脚下拉
此处我用的F4时钟频率168MHZ,进行168分频,计数频率1mhz
四个通道均设置为上升沿捕获,滤波8
生成代码时选择新建c、h文件
配置完成后开始写代码
在生成的tim.c的最后一行/* USER CODE BEGIN 1 */ /* USER CODE END 1 */中编写中断函数
以下是捕获中断函数
uint32_t tim8_ch1_t,tim8_ch2_t,tim8_ch3_t,tim8_ch4_t;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
//判断定时�?
if(TIM8 == htim->Instance){
static uint32_t tim8_ch1_start,tim8_ch1_stop;
static uint32_t tim8_ch2_start,tim8_ch2_stop;
static uint32_t tim8_ch3_start,tim8_ch3_stop;
static uint32_t tim8_ch4_start,tim8_ch4_stop;
static unsigned char tim8_ch1_flag=0;
static unsigned char tim8_ch2_flag=0;
static unsigned char tim8_ch3_flag=0;
static unsigned char tim8_ch4_flag=0;
//通道1捕获
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
tim8_ch1_flag++;
if(tim8_ch1_flag==1){
tim8_ch1_start = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_1);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_1);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_1, TIM_ICPOLARITY_FALLING);/* 配置TIM8通道1下降沿捕�?? */
}
if(tim8_ch1_flag==2){
tim8_ch1_stop = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_1);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_1);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_1, TIM_ICPOLARITY_RISING);/* 配置TIM8通道1上升沿捕�?? */
tim8_ch1_t = tim8_ch1_stop-tim8_ch1_start;
printf("ch1=%ld\n",tim8_ch1_t);
tim8_ch1_flag=0;
}
}
//通道2捕获
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
tim8_ch2_flag++;
if(tim8_ch2_flag==1){
tim8_ch2_start = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_2);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_2);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_2, TIM_ICPOLARITY_FALLING);/* 配置TIM8通道1下降沿捕�?? */
}
if(tim8_ch2_flag==2){
tim8_ch2_stop = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_2);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_2);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_2, TIM_ICPOLARITY_RISING);/* 配置TIM8通道1上升沿捕�?? */
tim8_ch2_t = tim8_ch2_stop-tim8_ch2_start;
printf("ch2=%ld\n",tim8_ch2_t);
tim8_ch2_flag=0;
}
}
//通道3捕获
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
{
tim8_ch3_flag++;
if(tim8_ch3_flag==1){
tim8_ch3_start = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_3);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_3);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_3, TIM_ICPOLARITY_FALLING);/* 配置TIM8通道1下降沿捕�?? */
}
if(tim8_ch3_flag==2){
tim8_ch3_stop = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_3);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_3);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_3, TIM_ICPOLARITY_RISING);/* 配置TIM8通道1上升沿捕�?? */
tim8_ch3_t = tim8_ch3_stop-tim8_ch3_start;
printf("ch3=%ld\n",tim8_ch3_t);
tim8_ch3_flag=0;
}
}
//通道4捕获
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
{
tim8_ch4_flag++;
if(tim8_ch4_flag==1){
tim8_ch4_start = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_4);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_4);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_4, TIM_ICPOLARITY_FALLING);/* 配置TIM8通道1下降沿捕�?? */
}
if(tim8_ch4_flag==2){
tim8_ch4_stop = HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_4);
TIM_RESET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_4);
TIM_SET_CAPTUREPOLARITY(&htim8, TIM_CHANNEL_4, TIM_ICPOLARITY_RISING);/* 配置TIM8通道1上升沿捕�?? */
tim8_ch4_t = tim8_ch4_stop-tim8_ch4_start;
printf("ch4=%ld\n",tim8_ch4_t);
tim8_ch4_flag=0;
}
}
}
//定时�?9捕获
if(TIM9 == htim->Instance){
static uint32_t tim9_ch1_start,tim9_ch1_stop;
static uint32_t tim9_ch2_start,tim9_ch2_stop;
static unsigned char tim9_ch1_flag=0;
static unsigned char tim9_ch2_flag=0;
//定时�?9通道1捕获
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
tim9_ch1_flag++;
if(tim9_ch1_flag==1){
tim9_ch1_start = HAL_TIM_ReadCapturedValue(&htim9, TIM_CHANNEL_1);
TIM_RESET_CAPTUREPOLARITY(&htim9, TIM_CHANNEL_1);
TIM_SET_CAPTUREPOLARITY(&htim9, TIM_CHANNEL_1, TIM_ICPOLARITY_FALLING);/* 配置TIM9通道1下降沿捕�?? */
}
if(tim9_ch1_flag==2){
tim9_ch1_stop = HAL_TIM_ReadCapturedValue(&htim9, TIM_CHANNEL_1);
TIM_RESET_CAPTUREPOLARITY(&htim9, TIM_CHANNEL_1);
TIM_SET_CAPTUREPOLARITY(&htim9, TIM_CHANNEL_1, TIM_ICPOLARITY_RISING);/* 配置TIM9通道1上升沿捕�?? */
tim9_ch1_t = tim9_ch1_stop-tim9_ch1_start;
printf("ch1=%ld\n",tim9_ch1_t);
tim9_ch1_flag=0;
}
}
}
}
编写一个溢出中断函数:
//定时器溢出回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(TIM8 == htim->Instance){
//每次溢出时间65536us
printf("定时器8溢出\n");
}
if(TIM9 == htim->Instance){
//每次溢出时间65536us
printf("定时器9溢出\n");
}
}
此时的定时器8捕获还不能正常使用,需要在主函数初始化打开捕获通道
HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_1); //�???启输入捕获中�???
HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_2); //�???启输入捕获中�???
HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_3); //�???启输入捕获中�???
HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_4); //�???启输入捕获中�???
也可以打开溢出中断也可以不打开