Timeline for What to do to stop PWM after N pulses in STM32?
Current License: CC BY-SA 4.0
25 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 2, 2023 at 11:25 | comment | added | antipattern | Its a bit out of scope of the Q, but when adapting this answer to work on TIM1 one is faced with the dilemma that TIM1 has not only the global interrupt but four distinct ones (TIM1 break and TIM9 global int., TIM1 update and TIM10 global int., TIM1 trigger and commutation and T11 global int., TIM1 capture compare int)... What's the appropriate interrupt to use in this case? | |
| Feb 4, 2022 at 9:06 | vote | accept | cm64 | ||
| Jan 28, 2022 at 13:23 | comment | added | cm64 | See my edit. I shared everything there. The PWM starts when PB1 is HIGH. But never stops. I might be using your code wrong way. I hope you might have a look at this. This is the last step to achieve my goal. Thanks anyways. | |
| Jan 28, 2022 at 13:13 | comment | added | cm64 | I will edit and share the files and config. | |
| Jan 28, 2022 at 13:13 | comment | added | cm64 | I tried that as well but PWM starts but never stops. It seems HAL_TIM_PWM_PulseFinishedCallback is not called at all. | |
| Jan 28, 2022 at 12:34 | comment | added | ChrisD91 | You're using HAL_TIM_PWM_Start_IT to start the PWM but to stop the PWM you're using HAL_PWM_Stop. You need at least to use HAL_TIM_PWM_Stop_IT. Check the driver file again and use the correct stop function for your code. You are using PWM in interrupt mode! | |
| Jan 28, 2022 at 12:28 | comment | added | cm64 | All is here: file.io/oLYzqsDrS7S6 I would appreciate this last bit or here easyupload.io/wg4mkz | |
| Jan 28, 2022 at 12:27 | comment | added | cm64 | It is digital input with voltage applied. I apply 3V with function generator to a digital pin. I set PB1 as GPIO_Ext1 with rising edge. I uploaded the project | |
| Jan 28, 2022 at 12:23 | comment | added | ChrisD91 | If the event starts correctly but never stops after your specific number of pulses, you're not stopping the PWM correctly or you're not counting the number of pulses correctly. You never stated how you're getting the digital input, if it's from a button you may also be getting a number of triggers on the interrupt due to bouncing. There's too many things to consider without knowing everything about your project and what you want to do. | |
| Jan 28, 2022 at 12:12 | comment | added | cm64 | Here is the entire project: file.io/iybKyE7hhkx5 | |
| Jan 28, 2022 at 12:06 | comment | added | cm64 | And in main I have this: void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) { if(htim -> Instance == TIM2) { if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { global_pulse_flag++; if(global_pulse_flag > 55){ HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1) ; } } } } | |
| Jan 28, 2022 at 12:06 | comment | added | cm64 | Thanks, I did that like this void EXTI1_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1); if(HAL_TIM_PWM_Start_IT(&htim2, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } } But now the event starts with digital input rising edge but never stops | |
| Jan 28, 2022 at 11:32 | comment | added | ChrisD91 | In that case, set your digital input to interrupt mode and enable the HAL_TIM_PWM_Start_IT in your digital interrupt handler. Stop the PWM in the pwm callback and repeat. | |
| Jan 28, 2022 at 11:30 | comment | added | cm64 | Works very well if I use reset button. Each time I push reset. All I need to do is is use digital input and obtain the same result. | |
| Jan 28, 2022 at 11:26 | comment | added | cm64 | But I need to trigger this event by a digital input. I mean the pulse train starts after a trigger input(3.3V to a digital input pin) and then stops after 1000 count. But next time with trigger or digital HIGH this can happen again. Should I reset something at the end of the callback? | |
| Jan 28, 2022 at 11:26 | comment | added | ChrisD91 | I would also not say if(global_flag == 1000) but (global_flag > 999 && global_flag < 1000) with an else statement to throw an error if it goes above 1000. That way you can catch the error | |
| Jan 28, 2022 at 11:24 | comment | added | cm64 | Okay I think I should use " if(global_pulse_flag == 1000){ HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1) ; }" | |
| Jan 28, 2022 at 11:18 | comment | added | cm64 | I generate the PWM now. But after global_pulse_flag++; I have now if(global_pulse_flag == 1000){ HAL_TIM_Base_Stop(&htim2); HAL_TIM_Base_Stop_IT(&htim2); } to stop PWM after 1000 counts. But PWM still runs I use timer2 in my case | |
| Jan 28, 2022 at 11:11 | comment | added | ChrisD91 | If you declare it inside the while loop, it will keep restarting the PWM initialisation every loop. Do it before the while loop, I'd recommend it in the /* USER CODE BEGIN 2 */ part the cubemx generates for you | |
| Jan 28, 2022 at 11:09 | comment | added | cm64 | Ok I will try. Will this: "if(HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_1) != HAL_OK)" be inside the while loop or before? | |
| Jan 28, 2022 at 10:49 | comment | added | ChrisD91 | Added a screenshot to the answer. In cubemx you need to configure your parameter settings (I assume you've already done this) and then in the NVIC settings tab you have the option to enable global interrupts for that timer | |
| Jan 28, 2022 at 10:47 | history | edited | ChrisD91 | CC BY-SA 4.0 | added 135 characters in body |
| Jan 28, 2022 at 10:42 | comment | added | cm64 | Interesting and I want to try this. But how do you set PWM as interrupt base in Cube MX? | |
| Jan 28, 2022 at 10:31 | history | edited | ChrisD91 | CC BY-SA 4.0 | added code for clarity |
| Jan 28, 2022 at 10:24 | history | answered | ChrisD91 | CC BY-SA 4.0 |