2024-12-15 10:13 PM - last edited on 2024-12-16 12:27 AM by SofLit
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.
2024-12-15 10:16 PM
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.
2024-12-16 05:30 AM
Hello @saurabhkore,
For TIM2, for the frequency_a calculation, you should use use t1a instead of t1
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.