2021-12-29 05:02 AM
I am trying to generate a fixed number of PWM Waves using stm32f407. I called both functions
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_1);
and in the ISR
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
pwmCount ++;
if (pwmCount == 1001){
HAL_TIM_Base_Stop_IT(&htim2);
HAL_TIM_PWM_Stop_IT(&htim2,TIM_CHANNEL_1);
}
}
The problem is if we have to generate n number of pulses we have to give n+1 in the condition(eg: if I want to generate 1000 pulses I have to give 1001) what will be the reason?
And also if check pwmCount ==1 (for generating a single pulse) it continuously gives the pulses.
Anyone please clarify these two doubts
2021-12-29 06:36 AM
Perhaps you don't initialize pwmCount correctly, or that you don't mark it volatile.
Also note there is some lag between an ISR being triggered and its code being executed. If your pulses are short, you may be running into this. But since pwmCount==1 fails to trigger, it suggests this isn't the main issue.
Alternatively, chained timers can be used to generate a specified number of pulses without CPU intervention.
2021-12-29 07:48 AM
Debug?
How you init pwmCount on start = 0 ? Then is normal 0..1000 is 1001 cycles usw
2021-12-29 12:30 PM
In the timer setup routine, Cube/HAL uses TIMx_EGR.UG to force prescale settings to be accepted immediately, and this sets TIMx_SR.UIF. Later, when
> HAL_TIM_Base_Start_IT(&htim2);
is called, Update interrupt is enabled, that immediately causes the ISR to be executed, the Cube/HAL ISR "resolver" calls HAL_TIM_PeriodElapsedCallback() which in case of pwmCount ==1 immediately also disables the Update interrupt, so there is nothing . Only after that
> HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_1);
is called, starting PWM generation, and there's nothing which would stop it afterwards.
The 1000/1001 has the same cause.
JW
2021-12-29 08:50 PM
I initialized the pwmCount = 0 . I am generating pwm pulse of 2.5Hz (400ms).
Sorry Sir I don't know chained timers . Can you please tell me what is it
2021-12-29 08:53 PM
I have done initialization with pwmCount = 0;It trigger the HAL_TIM_PeriodElapsedCallback() ISR, after generating 1st pulse ?
2021-12-29 09:34 PM
The HAL_TIM_PeriodElapsedCallback() is called immediately after calling HAL_TIM_Base_Start_IT(&htim2)
when the timer reaches the value stored in the 'reload register, it again trigger the ISR.
so when the timer reaches the value stored in the 'reload register' first time, ISR is triggered two times ?
2021-12-30 04:05 AM
> so when the timer reaches the value stored in the 'reload register' first time, ISR is triggered two times ?
I'd formulate it in this way: When the timer reaches the value stored in the 'reload register' first time, ISR is triggered the second time.
JW