cancel
Showing results for 
Search instead for 
Did you mean: 

Reconfigure the timer at runtime

diverger
Associate III
Posted on May 24, 2016 at 16:03

I'm use STM32CubeMX to configure TIM2 CH1 to compare output, and it works well. Now I want to change it to PWM output when the programm running. Then which HAL library function to call to clear the registers previous set? It seems if I directly configure it without clear the registers, the value written before can't be cleared. My target MCU are STM32F429I. 

Thanks.

#timer
3 REPLIES 3
carmine
Associate II
Posted on May 24, 2016 at 16:28

You need to enable the preloading of shadow register by setting the TIM_CR1_ARPE in TIMx->CR1 register. It seems that the HAL doesn't provide a way to do this currently, so you have to configure it manually:

TIM3->CR1 |= TIM_CR1_ARPE;  //Enable preloading

TIM3->CR1 &= ~TIM_CR1_ARPE; //Disable preloading

This will cause that the ARR is updated when the timer expires.

diverger
Associate III
Posted on May 24, 2016 at 16:56

Thanks. But in my situation, I can't even clear the setting values in CRx written previously. The bit I set before can't be reset. Why? Do I need manually clear them?

Walid FTITI_O
Senior II
Posted on May 24, 2016 at 17:37

Hi diverger,

Try , first, to reset the output compare mode bits , select PWM mode and then enable the output compare preload. See code below:

/* SET PWM1 mode */ 
/* Reset the Output Compare Mode Bits */ 
TIM1->CCMR1 &= ~TIM_CCMR1_OC1M; 
TIM1->CCMR1 &= ~TIM_CCMR1_CC1S; 
/* Select the output compare mode 1*/ 
TIM1->CCMR1 |= TIM_OCMODE_PWM1; 
/* Enable the output compare 1 Preload */ 
TIM1->CCMR1 |= TIM_CCMR1_OC1PE; 

-Hannibal-