cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 - TIM1 PWM Interrupt not working?

ReV Hulk
Associate
Posted on April 17, 2017 at 17:56

Hi, I'm using STM32F103RBTx MCU and I generate my code using CubeMX (v4.20.1).

I need PWM output on 2 Pin: Timer1 - Channel 1 and Timer4 - Channel 4.

These 2 PWM signal is used to control 2 separated DC motors.

I configure them both with Internal Clock and selected the correct PWM Channel output.

In the Configuration Tab, I select the Prescaler & Counter Period for both timers (PWM Mode 1).

Since I need to use the interrupt to toggle some LEDs, so I enable only 'TIM1 Update Interrupt' and 'TIM4 Global Interrupt'.

In my code, after the some initialization functions from HAL. I started each Timer with:

MX_TIM1_Init();

MX_TIM4_Init();

//.......

......//

HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1);

HAL_TIM_PWM_Start_IT(&htim4, TIM_CHANNEL_4);

And with the Interrupt Handler, I used:

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim){

      if (htim -> Instance == htim1.Instance){                         //Timer1-CH1

            HAL_GPIO_TogglePin(GPIOB, OP_LEDA_Pin);      //Toggle LEDA

      }else{                                                                              //Timer4-CH4

            HAL_GPIO_TogglePin(GPIOB, OP_LEDB_Pin);      //Toggle LEDB

      }

}

With my setup, both DC motors received its correct PWM signal. However, only LEDB toggles, LEDA doesn't.

When I debug, the pointer only stop inside htim4.Instance, which means Timer4 works as expected. But it just doesn't stop inside htim1.Instance. I don't know what I'm missing with Timer1?

Could anyone help me figure out what I did wrong?

Regards,

ReV.

2 REPLIES 2
Posted on April 17, 2017 at 21:15

There is a different TIM1 CCx Interrupt, separate from the Update one

DCD TIM1_BRK_IRQHandler ; TIM1 Break

DCD TIM1_UP_IRQHandler ; TIM1 Update

DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation

DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ReV Hulk
Associate
Posted on April 21, 2017 at 16:01

Thanks Clive,

I switch to TIM1_Up_IRQHandlder and everything work. It just that now I have to start both timer as counter along with the PWM function.

Still I don't understand why TIM1 doesn't work in 

HAL_TIM_PWM_PulseFinishedCallback.