cancel
Showing results for 
Search instead for 
Did you mean: 

How can measure PWM frequency and duty cycle using stm32f0?

kalpeshpatil102
Associate III
Posted on February 16, 2017 at 05:52

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

6 REPLIES 6
Posted on February 16, 2017 at 11:44

The STM32 has an PWM Input mode pairing two channels of the TIM and doing a reset, that allow it to read period and duty directly from the channel capture registers.

If you want a high frequency time base, use a free running TIM and read the count register. You could use 1 MHz for microsecond granularity or faster if you need.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on February 17, 2017 at 06:34

Turvey.Clive.002

‌ thank you for reply. Can you give anyexample for understanding

micros() function.

Posted on February 17, 2017 at 09:41

Hi

Patil.Kalpesh

,

Please refer to the example 'TIM_PWMInput' available when you download the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef0.html

package.

It shows how to use the TIM peripheral to measure the frequency and the duty cycle of an external signal.

The example is found under the path below:

STM32Cube_FW_F0_V1.7.0\Projects\STM32F0...\Examples\TIM\TIM_PWMInput

Khouloud.

Posted on February 17, 2017 at 20:15

Sorry not doing HAL stuff.

Generally for F0 doing micro-second timing, you'd likely want to set up a maximal TIM counting at 1 MHz, and read the TIMx->CNT value

uint16_t start = TIMx->CNT;

while((TIMx->CNT - start) < micros);

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
kalpeshpatil102
Associate III
Posted on February 20, 2017 at 05:28

thank you

Turvey.Clive.002

‌ and

garsi.khouloud

. i will go through your suggestion and up[date my answer.

sorry for my late reply .

thank you

Posted on February 20, 2017 at 07:45

if you still need help, I have the same footprint as you do, and I use HAL