cancel
Showing results for 
Search instead for 
Did you mean: 

Changing Timer Capture Compare from Timer ISR

joshmulder
Associate
I am trying to change the PWM duty cycle of timer3, channel 1 from the period elapsed ISR of timer 6. This seems to cause the chip to crash.
 
I can confirm that:
- timer6 is properly configured and interrupt fires appropriately.
- timer3 is properly configured and is outputting PWM (duty cycle can be changed normally from main loop)
 
Using stm32f103vct6
 
Any ideas?
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
         if (htim->Instance == TIM6)
         {   
                 __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1800);
         } 
}
2 REPLIES 2
LCE
Principal

What's exactly happening? "The chip crashes" is rather vague... Debugged?

Did you check:

- the reference manual, any prereqs for changing a CCR register on the fly?

- the macro __HAL_TIM_SET_COMPARE ?

- try without the macro, some direct register setting, should be something like "TIM3->CCR1 = 1800;"

- sure that ARR (?) > 1800 ?

The timers are not too hard to handle via direct register settings, maybe check that instead of HAL / MX stuff.

EXUE.2
ST Employee

I guess It is because to update the TIM3 configuration too quickly, you can try to control the duration of this setting or add one flag here and move "__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_11800);" to main(), like below:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
         if (htim->Instance == TIM6)
         {   
                 MAIN_TIM3_COMPARE_FLAG = 1;
         } 
}
 
void main(void)
{
  if(MAIN_TIM3_COMPARE_FLAG == 1)
  {
      __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1800);
      MAIN_TIM3_COMPARE_FLAG = 0;
  }
}