2017-02-15 08:52 PM
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
2017-02-16 02:44 AM
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.
2017-02-16 10:34 PM
Turvey.Clive.002
thank you for reply. Can you give anyexample for understandingmicros() function.
2017-02-17 01:41 AM
Hi
Patil.Kalpesh
,Please refer to the example 'TIM_PWMInput' available when you download the
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.
2017-02-17 12:15 PM
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);
2017-02-19 08:28 PM
thank you
Turvey.Clive.002
andgarsi.khouloud
. i will go through your suggestion and up[date my answer.sorry for my late reply .
thank you
2017-02-19 11:45 PM
if you still need help, I have the same footprint as you do, and I use HAL