AnsweredAssumed Answered

How can measure PWM frequency and duty cycle using stm32f0?

Question asked by patil.kalpesh on Feb 16, 2017
Latest reply on Feb 20, 2017 by T J

Hello, I am using stm32f0 and cube mx for source code generation and keil 5MDK for programming .I am new in stm32f0

In my project i want to measure pwm duty cycle and frequency. I can measure frequency by using HAL_GetTick(); function. the measure frequency is not 100% accurate.  my code for frequency  below 

 

while (1)
{

 


while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)==0)
{
}
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)){
start_time=HAL_GetTick();
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
}
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)==0){
}
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)){
end_time=HAL_GetTick();
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
total_time=(end_time-start_time);
frequency=1000/total_time;
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
//total_time=0;
}

}

 

 

I didn't understand how can measure frequency and duty cycle using timer input capture function. and how can read pwm frequency and duty cycle ?

I have another question is there are millis() & micros()  function in arduino .  I know stm32 HAL_GetTick(); is like arduino millis().How can  measure time in microsecond  in stm32f0  ?

sorry for poor English 

please help me 

thank you

Outcomes