cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with duty cycle measurement

Samuel1
Associate III

Hello everybody,

I wanted to measure the duty cycle (tone) of a PWM signal with input capture mode. Unfortunately, it works only almost up to 65% of the duty cycle(Ton). otherwise will

the T_off measured instead of Ton. does anyone have an idea what can i do to solve this

problem?

the timer has a clock frequency of 1 MHz.

 #TIMER​ #PWM​ 

HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
HAL_TIM_Base_Start(&htim5);
HAL_TIM_IC_Start_IT(&htim5, TIM_CHANNEL_1);
 
 
 
 
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(htim);
 
if(htim==&htim5){
//  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
  capture= HAL_TIM_ReadCapturedValue(&htim5,TIM_CHANNEL_1);
  __HAL_TIM_SetCounter(&htim5,0);
}
}

1 REPLY 1
S.Ma
Principal

If you can use 2 channels to the incoming signal, one capture the rise and the other the fall.

Otherwise, capture on both edges under interrupt and read the pin level to know if it was a fall or rise edge.

The frequency of the signal to measure should be must smaller than the SYSCLK (interrupt need some short time to proceed).

Substract the last rise by fall capture for the high time, the rise by previous rise for the period.