cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Change timer period in code

Bob Cameroon
Associate II
Posted on November 30, 2017 at 20:25

Hello

I'm using TIM3 on my STM32F4. Currently TIM3 period is set on 500 (using CubeMX). Is it possible to change it with pressing 'User button'?

For example

'if (currentPress == 1) {

      htim3.Init.Period = 200;

} else if (currentPress == 2) {

      htim3.Init.Period = 400;

...

}'

I guess it could be done via TIM3->.... but I'm not sure how to make it, because I'm beginner.

What I was trying to do was using TIM3->CCR1 = 200, etc.. but it seems like it doesn't work.

Thanks for help in addition.

5 REPLIES 5
Posted on December 01, 2017 at 08:12

TIM3->ARR = 200 - 1; // Remember count counts 0 thru N-1, ie N counts

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 01, 2017 at 18:36

Sounds good, but works only for 3-4 clicks. After that Led starts to shine all the time, or doesn't shine at all untill I press reset button. 

Any other ideas?
Posted on December 01, 2017 at 18:43

Check the timer overflow value is updated at overflow (I think it's about shadowing). If not, changing the max value from 400 to 200 when the timer is at 300 may get the timer stuck. Some timer spec reading needed.

Posted on December 01, 2017 at 18:53

Probably wouldn't want to enable 'Preload', in the normal mode the new ARR is held in a holding/shadow register and loaded into the active one at the Update event (ie CNT == 0).

If preload is enabled it basically writes through directly, then you'd want to be wary of where CNT is in the cycle

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 01, 2017 at 19:17

Centauris.Alpha

‌ and

Turvey.Clive.002

‌. Thank you both for you answer. Since I'm total beginner (only a week or two) I don't have much idea how to make it. But thanks for ideas, I'm gonna check out what else I can do.