cancel
Showing results for 
Search instead for 
Did you mean: 

HAL - Why is the PWM pulse callback not triggering?

JWyli.1
Associate II

Hi there,

I would like to execute some code in an interrupt handler when the pulse value for TIM16 is reached. But for some reason, none of the interrupts provided by HAL seem to trigger for this event.

I have setup the timer using the configuration tool with the following:

(TIM 16 running off an 8MHz clock)

CH 1 - PWM Generation CH1

Prescaler - 799

ARR - 9999

Mode - PWM mode 1

Pulse - 2499

I assigned pin PB8 to produce the PWM signal.

To start the timer I have made the call:

/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start_IT(&htim16, TIM_CHANNEL_1);
/* USER CODE END 2 */

I am using a scope to check that indeed there is a PWM signal on PB8 with a duty cycle of 25% and a period of 1s. As expected.

I have placed LED toggling code inside the following interrupts:

HAL_TIM_PeriodElapsedCallback

HAL_TIM_PeriodElapsedHalfCpltCallback

HAL_TIM_OC_DelayElapsedCallback

HAL_TIM_PWM_PulseFinishedCallback

HAL_TIM_PWM_PulseFinishedHalfCpltCallback

HAL_TIM_TriggerCallback

HAL_TIM_TriggerHalfCpltCallback

HAL_TIM_ErrorCallback

As I expected, the LED being toggled by HAL_TIM_PWM_PulseFinishedCallback is changing state every second.

The only other LED that is toggling is the one belonging to HAL_TIM_OC_DelayElapsedCallback. Evidently, all the other interrupts are not being called.

I would expect that "HAL_TIM_PWM_PulseFinishedHalfCpltCallback" would be called when the pulse value is reached in the CNT register of tim16, but this does not seem to be happening.

What is the "HAL_TIM_PWM_PulseFinishedHalfCpltCallback" used for? And if it is not to indicate when the pulse value is reached, then what callback can I use for this purpose?

Thanks in advance!

P.S. Here is a TIM16 register dump

0693W000003RFtSQAW.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

HAL_TIM_PWM_PulseFinishedCallback is called when the pulse value (CC1) is reached, so that's the one you want.

HAL_TIM_PeriodElapsedCallback is called when the timer updates. It is enabled with HAL_TIM_Base_Start_IT. It sounds like you may want this one as well?

HAL_TIM_PWM_PulseFinishedHalfCpltCallback is enabled in HAL_TIM_PWM_Sta1rt_DMA and similar and doesn't do what you want here.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
TDK
Guru

HAL_TIM_PWM_PulseFinishedCallback is called when the pulse value (CC1) is reached, so that's the one you want.

HAL_TIM_PeriodElapsedCallback is called when the timer updates. It is enabled with HAL_TIM_Base_Start_IT. It sounds like you may want this one as well?

HAL_TIM_PWM_PulseFinishedHalfCpltCallback is enabled in HAL_TIM_PWM_Sta1rt_DMA and similar and doesn't do what you want here.

If you feel a post has answered your question, please click "Accept as Solution".
JWyli.1
Associate II

It makes so much more sense now! Thanks so much!

I am little confused, Can u please elaborate on this statement

>> HAL_TIM_PeriodElapsedCallback is called when the timer updates. 

what does it mean when timer updates(The update event is flagged or something else).

Actual I am looking for a function callback end of each PWM completion.

Please guide me on which HAL function to use.

If I recall correctly:

HAL_TIM_PeriodElapsedCallback is called when the timer reaches the value stored in the 'reload register' (This is when the timer 'updates' and determines the frequency of your PWM signal)

As TDK has said above HAL_TIM_PWM_PulseFinishedCallback is called when the timer reaches the pulse value stored in the C&C register. This is responsible for your duty cycle.

Don't foget that if you want HAL_TIM_PeriodElapsedCallback, you must call HAL_TIM_Base_Start_IT and if you want HAL_TIM_PWM_PulseFinishedCallback, you must call HAL_TIM_PWM_Start_IT. To have both callbacks you must call both 'start' functions.

CWang.13
Associate II

Hey guys, I'm wondering where can we find a useful manual if we want to use those HAL functions? Like to know which timer register to use for functionalities and how settings should be done in .ioc file.

Find out the Reference manual from your Specific mcu or mcu family.

For example , the Ref manual for stm32f103xx would behttps://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwi8reLL7bb6AhV0gc4BHTE9AYwQFnoECBEQAQ&url=https%3A%2F%2Fwww.st.com%2Fresource%2Fen%2Freference_manual%2Fcd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf&usg=AOvVaw2kF0T1D3TzsgvgnX7fvMku

we dont need to firmware by ourselves, lets talk