2016-10-11 10:08 AM
Hello, I am having some difficulties with input capture on the nucleo64-r401re.
I manage to generate a PWM signal to a ultrasonic sensor, that replies correctly (checked with oscilloscope), however i do not manage to capture the returning data correctly it seems. The callback fires but the data it reads is always the same.Here is my code:http://paste.ofcode.org/wsXi8arKfbCxHGy3fBQBf9Any ideas to what I am missing?2016-10-11 11:28 AM
Hi paulstrom.bjorn,
I recommend that you cehck both example in the STM32CubeF4 wich are - * ''TIM_InputCapture'' at this path STM32Cube_FW_F4_V1.13.0\Projects\STM32F401RE-Nucleo\Examples\TIM\TIM_PWMInput * ''TIM_PWMInput'' example at this path STM32Cube_FW_F4_V1.13.0\Projects\STM32F401RE-Nucleo\Examples\TIM\TIM_PWMInputTry to compare with you own code and figure out what is missed there (check timer instance parameter, system and timer clocks..).-Hannibal-2016-10-18 05:51 AM
2016-10-18 09:14 AM
This looks to be using PWM Input, not Input Capture, as such value will keep resetting.
TIM2 should also be 32-bit, which would give you support for longer periods when clocking the counter at 84 MHz2016-10-18 09:32 AM
Hello clive1, and thank you for your answer :)
Yes you are correct, the period should be for 32bit instead of 16bit, forgot to change it from the STM examples which used TIM4 which is 16bit.I think the code uses both input capture and pwm input, both on different channels, input capture on channel 2, and pwm input on channel 1:void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
if (uwIC2Value != 0)
{
/* Duty cycle computation */
uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value;
/* uwFrequency computation
TIM4 c
ounter clock = (RCC_Clocks.HCLK_Frequency) */
uwFrequency = (HAL_RCC_GetHCLKFreq()) / uwIC2Value;
}
else
{
uwDutyCycle = 0;
uwFrequency = 0;
}
}
}
That atleast is how I understood the example but my experience with IC and PWM is limited. Or have I misunderstood you and you mean the example only sets up a PWM input? How would i go about changing this to use input capture then?2016-10-18 09:59 AM
Can't really help you with HAL, not my deal.
The Channels latch the counter, in PWM Input the counter gets reset, so you have a pair of channels one determining the total period (ie rising to rising) and the other the duty (rising to falling). To understand what might be happening, and why it might be wrong, I'd need to understand the nature/frequency of the input signal, and the measurements you are observing for them.