cancel
Showing results for 
Search instead for 
Did you mean: 

How to use multiple timer capture Interrupt in stm32

saurabhkore
Associate III

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. 

1 REPLY 1
saurabhkore
Associate III

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.