cancel
Showing results for 
Search instead for 
Did you mean: 

pwm duty cycle measurement for 0% duty cycle

saurabhkore
Associate III

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

  }
}

 

 

24 REPLIES 24

I suggest op try to reset the pwm variable reset to zero in the while(1) loop which is protected by a flag

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

Great sir, it is working now. I implement watchdog timer here. little bit modification required then it will work properly.

Thankyou.

hello sir.

Conclusion:

I implement watchdog inside capture callback function, now it is working fine. but when watch dog timer reset then  mcu also reset and whole program restart but if we take reset time of watch dog timer very small like 10msec or 20 msec then it will not affect any things.so this is conclusion from side. if want to add anything then please share your view .and thanks to all members who suggests their valuable suggestions for this problem.

Thankyou.


@saurabhkore wrote:

when watch dog timer reset then  mcu also reset and whole program restart .


Of course it does - that is the defined purpose of the Watchdog.

I didn't say to use the MCU's Watchdog - I said that your timeout function needs to work like a watchdog:

 


@Andrew Neil wrote:

Think of it like a "Watchdog" timer: each time you get a capture callback, you restart the "watchdog".

Therefore it will only time-out after there have been no capture callbacks for the timeout time.


 

yes, sir correct i implement this logic also but not worked for me.