stm32使用hal库定时器8,四路输入捕获测量高电平时间

此处我们使用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);    //�???启输入捕获中�???

也可以打开溢出中断也可以不打开

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用STM32HAL编写代码时,设置编码器的方向可以通过以下步骤完成: 1. 配置GPIO引脚:首先,需要将编码器的A相和B相信号引脚与STM32微控制器的GPIO引脚连接。使用HAL提供的函数(如HAL_GPIO_Init)初始化这些GPIO引脚。 2. 配置编码器接口模式:接下来,通过调用HAL提供的函数(如HAL_TIM_Encoder_Init)初始化编码器接口模式。这将配置定时器和GPIO引脚以捕获编码器的脉冲信号。 3. 配置编码器方向:通过调用HAL提供的函数(如HAL_TIM_Encoder_Start)启动编码器接口,并设置捕获方向。要设置编码器的方向,请使用HAL提供的函数(如HAL_TIM_Encoder_SetPolarity)设置定时器输入极性。 例如,以下代码片段演示了如何使用STM32HAL配置编码器的方向: ```c // 配置GPIO引脚 GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOA_CLK_ENABLE(); // 使能GPIOA时钟 GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; // 编码器A相和B相引脚 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // 输入模式 GPIO_InitStruct.Pull = GPIO_PULLUP; // 上拉模式 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化GPIOA引脚 // 配置编码器接口模式 TIM_HandleTypeDef htim; __HAL_RCC_TIM2_CLK_ENABLE(); // 使能TIM2时钟 htim.Instance = TIM2; htim.Init.EncoderMode = TIM_ENCODERMODE_TI12; // 编码器接口模式 htim.Init.IC1Polarity = TIM_ICPOLARITY_RISING; // A相信号极性 htim.Init.IC2Polarity = TIM_ICPOLARITY_RISING; // B相信号极性 htim.Init.IC1Selection = TIM_ICSELECTION_DIRECTTI; // 直接输入模式 htim.Init.IC2Selection = TIM_ICSELECTION_DIRECTTI; // 直接输入模式 HAL_TIM_Encoder_Init(&htim); // 初始化编码器接口 // 配置编码器方向 HAL_TIM_Encoder_Start(&htim, TIM_CHANNEL_ALL); // 启动编码器接口 HAL_TIM_Encoder_SetPolarity(&htim, TIM_ENCODERPOLARITY_NONINVERTED); // 设置编码器方向为非反转 ``` 请根据您的硬件连接和需求进行适当的修改。此代码片段假定编码器的A相和B相信号连接到GPIOA的引脚0和1,使用TIM2作为编码器接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值