Skip to main content
Graduate II
December 16, 2024
Question

How to use multiple timer capture Interrupt in stm32

  • December 16, 2024
  • 1 reply
  • 693 views

Hii....i AM WORKING ON stm32l475rg MCU. i want to use two different timers for measurement of two different PWM inputs signals. for this one i am using capture call back function but i am facing problem to measure duty cycle of both channels at a time.

 

 

#include "main.h"
float t1=0.00;
float t2=0.00;
uint32_t frequency=0;
volatile float duty_c=0.00;

float t1a=0.00;
float t2a=0.00;
uint32_t frequency_a=0;
volatile float duty_c_a=0.00;

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
 void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{

	 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
	 {

	 t1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
	 if (t1 != 0)
	 {
	 t2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
	 frequency=80000000/t1;
	 duty_c=(t2/t1)*100.00;
	 }
	 }
 }
	else if(htim->Instance == TIM2)
 {
	 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
	 {
	 t1a = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
	 if (t1a != 0)
	 {
	 t2a = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
	 duty_c_a = (t2a / t1a) * 100.0;
	 frequency_a=80000000/t1;
	 }

	 }
 }
}

int main(void)
{

 HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
 HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
 HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
 HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
}

 

 

i share my code here. i use timer2 and timer 3 for measurement of pwm duty cycle of two different pwm input. 

    This topic has been closed for replies.

    1 reply

    Graduate II
    December 16, 2024

    By typing mistake only in above code i write two captures call back function please refer

    #include "main.h"
    float t1=0.00;
    float t2=0.00;
    uint32_t frequency=0;
    volatile float duty_c=0.00;
    
    float t1a=0.00;
    float t2a=0.00;
    uint32_t frequency_a=0;
    volatile float duty_c_a=0.00;
    
    void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
    {
    	if(htim->Instance == TIM3)
    	{
    
    	 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
    	 {
    
    	 t1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
    	 if (t1 != 0)
    	 {
    	 t2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
    	 frequency=80000000/t1;
    	 duty_c=(t2/t1)*100.00;
    	 }
    	 }
     }
    	else if(htim->Instance == TIM2)
     {
    	 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
    	 {
    	 t1a = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
    	 if (t1a != 0)
    	 {
    	 t2a = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
    	 duty_c_a = (t2a / t1a) * 100.0;
    	 frequency_a=80000000/t1;
    	 }
    
    	 }
     }
    }
    
    int main(void)
    {
    
     HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
     HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
     HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
     HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
    }

    following code. 

    ST Employee
    December 16, 2024

    Hello @saurabhkore

    For TIM2, for the frequency_a calculation, you should use use t1a instead of t1