cancel
Showing results for 
Search instead for 
Did you mean: 

pwm duty cycle measurement for 0% duty cycle

saurabhkore
Associate II

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)
  {

  }
}

 

 

11 REPLIES 11
Techn
Senior III

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.

 

If you feel a post has answered your question, please click "Accept as Solution".

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.