2024-05-01 10:57 PM
Hi Team,
We use stm32f303Rbt6 microcontroller. I need a output for Input capture, I am using two timers. I generate pulse for Tim1(1khz) and I set for Tim2 for input capture mode(input capture direct mode). TIM1 generate the pulse for continuously(1khz). But TIM2 set the mode for frequency capture in rising edge only. But we did not get the output and I enable the global interrupt for TIM2. But HAL_TIM_IC_CaptureCallback function does not woring my code. So we did not get the output for Frequency. So I'm already try this method to develop my code. I attach my code and I made a mistake pls correct me.
TIM1 and TIM2 Configuration Pictures.
Solved! Go to Solution.
2024-05-02 08:51 AM
Hello @mrsmile,
1) There is an incorrect logical comparison in your code, both conditions are the same:
if(IC_Val2 > IC_Val1) // if the second value is higher
{
Difference = IC_Val2 - IC_Val1;
}
else if(IC_Val2 > IC_Val1)
{
Difference = ((0xffff - IC_Val1) + IC_Val2) +1;
}
The second condition should be if(IC_Val1 > IC_Val2) to handle the timer overflow correctly.
2) Also, your Ref_clock calculation seems arbitrary?
3) You are resetting the counter of htim in the callback wrongly here:
__HAL_TIM_SET_COUNTER(htim, 0); // reset the counter
you should specify the timer instance explicitly, like __HAL_TIM_SET_COUNTER(&htim3, 0);
4) You start the PWM with HAL_TIM_PWM_Start and then again with HAL_TIM_PWM_Start_IT for the same channel here
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // PWM_GENERATE_TIM1_15KHZ
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2); // INPUT_CAPTURE_PWM_IT_START
HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1);
You should only start it once!
5) The variables i and hi are declared but not used in your code.
6) Make sure that the global interrupt for TIM2 is enabled in NVIC, and verify they are correctly configured in terms of prescaler, clock source,..
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-05-02 08:51 AM
Hello @mrsmile,
1) There is an incorrect logical comparison in your code, both conditions are the same:
if(IC_Val2 > IC_Val1) // if the second value is higher
{
Difference = IC_Val2 - IC_Val1;
}
else if(IC_Val2 > IC_Val1)
{
Difference = ((0xffff - IC_Val1) + IC_Val2) +1;
}
The second condition should be if(IC_Val1 > IC_Val2) to handle the timer overflow correctly.
2) Also, your Ref_clock calculation seems arbitrary?
3) You are resetting the counter of htim in the callback wrongly here:
__HAL_TIM_SET_COUNTER(htim, 0); // reset the counter
you should specify the timer instance explicitly, like __HAL_TIM_SET_COUNTER(&htim3, 0);
4) You start the PWM with HAL_TIM_PWM_Start and then again with HAL_TIM_PWM_Start_IT for the same channel here
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // PWM_GENERATE_TIM1_15KHZ
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2); // INPUT_CAPTURE_PWM_IT_START
HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1);
You should only start it once!
5) The variables i and hi are declared but not used in your code.
6) Make sure that the global interrupt for TIM2 is enabled in NVIC, and verify they are correctly configured in terms of prescaler, clock source,..
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-05-03 12:03 AM - edited 2024-05-03 12:14 AM
1) I correct the logical comparison in my code and attached the Image.
2) My reference clock calculation not arbitary. reference clock TIM3 and APB1 36 MHZ.
I generate the frequency in tim3 for PSC-719, ARR-65535, clk - 72000000, frequency- 1.5 hz.
3) Yes, are resetting the counter of htim in the callback. Correct my mistake and change my code.
__HAL_TIM_SET_COUNTER(&htim3, 0); // reset the counter
4) Yes, start the PWM with HAL_TIM_PWM_Start and generate the pulse in TIM1_CH1.But I did not use the interrupt handler HAL_TIM_PWM_Start_IT. I am using only this HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2) call back fucntion only.
5) sure that the global interrupt for TIM3 is enabled in NVIC.
it's my code was working but counter values did'nt get the rising edge.call back function also called but output did not come. so am stuck this stage. so what can I do for next step.
If your any possible way to provide sample code for inpu capture STM32f303RBt6 this Microcontroller.
I attach my code Once again.
2024-05-03 02:39 AM
Hello again @mrsmile,
Please check this InputCapture example project in STM32F3CubeFW
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-05-03 05:24 AM
this is not working. Can you share any other example programme references for stm32f303rbt6.