2015-01-27 09:27 AM
Hello everyone,
I am new to STM32f4 board. I would like to measure speed of a dc motor(in terms of input signal frequency which varies from 0 Hz to 50KHz). Actually I have only one signal (so I think I can't use encoder mode of stm32 timer.??). Can someone help me..?? Is there any way by which I can use timer in encoder mode..??2015-01-27 10:23 AM
Input Capture or Input PWM might be more effective by measuring period/duty. You can also count external pulses and integrate over time.
2015-01-28 12:43 AM
thanks clive1 for reply,
is there a way to measure the time between two edges (between rising and falling edge), I am asking this because my input frequency is changing too fast ??2015-01-28 01:05 AM
You can do it using two channels, one for each edge.
See the ''PWM input mode'' chapter in manual - the principle is the same, except you don't reset the counter this time. JW2015-01-28 03:10 AM
hi waclawek.jan,
I tried with PWM input mode, but the problem is it can not measure the freq below 1280 Hz. But in my application i need to measure the frequencies upto 20 Hz.? So can u please tell me correction in the following code.?? code: TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM4, &TIM_ICInitStructure); /* Select the TIM4 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM4, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE); void TIM4_IRQHandler(void) { RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); /* Clear TIM4 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM4, TIM_IT_CC2); /* Get the Input Capture value */ IC2Value = TIM_GetCapture2(TIM4); if (IC2Value != 0) { /* Duty cycle computation */ DutyCycle = (TIM_GetCapture1(TIM4) * 100) / IC2Value; /* Frequency computation TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */ Frequency = (RCC_Clocks.HCLK_Frequency)/2 / IC2Value; } else { DutyCycle = 0; Frequency = 0; } } In above code I tried changing prescalar value. but that was of no use.2015-01-28 04:34 AM
I tried with PWM input mode, but the problem is it can not measure the freq below
1280 Hz. But in my application i need to measure the frequencies upto 20 Hz.?
So can u please tell me correction in the following code.?? Certainly should be capable of 20 Hz, but thought you said 50 KHz earlier? For 16-bit timers you'd need to be conscious of the timebase configuration, and the wrap time of the counter. Code fragments really aren't helpful, post complete and concise example code that can be compiled. Use the Format Code Block tool (Paintbrush [<>] icon)