2024-12-03
04:09 AM
- last edited on
2024-12-03
07:06 AM
by
Maxime_MARCHETT
I am working on STM32L476RG mcu for duty cycle measurement of PWM signal. I share my code with you. it working fine but when i switch off pwm input coded stuck there and it shows previous reading .when i again given input then it measure the duty cycle correctly. so my question is that why code stuck at 0% duty cycle or when no PWM input provide to it. please check my code and share any solution .
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) // If the interrupt is triggered by channel 1
{
// Read the IC value
ICValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
if (ICValue != 0)
{
// calculate the Duty Cycle
Duty = (HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2) *100)/ICValue;
Frequency = 90000000/ICValue;
}
}
}
Int main(void)
{
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
HAL_TIM_IC_Start(&htim2, TIM_CHANNEL_2);
while(1)
{
}
}
2024-12-04 03:11 AM
What I suggest is to add a flag as volatile unit8_t dutyCycleData = 0 ; make it 1 when you get a new calculation time, copy the duty cycle value to a volatile variable say back_dutycycle. Read the backed duty cycle in the while(1) loop, clear the flag. only side effect is that you may get just the previous data, which many not be that bad since the duty cycle is measured in every cycle.
2024-12-04 03:18 AM - edited 2024-12-04 03:18 AM
And it's still getting "stuck" with that addition?
So what debugging have you done to see what's happening?
Is your "no edges" timer ever triggering?
Please re-post the code legibly.