Setting TIMER as 16MHz [STM32] - Page 1
Forum section: electronics / general

author: A.Ersoz
author_rank: Regular Contributor
timestamp: October 04, 2019, 07:38:09 pm
content: Hello,I want to get timer frequency between 16MHz to 20MHz. So I picked the TIM4 and set it with internal clock. Based on the datasheet (STM32F767), the timer belongs to APB1 bus. When I set the main clock frequency as 216Mhz, I expect to get 108MHz timer clock. For getting, 16MHz output, I set the prescaler 7-1 and counter period as 1-1. For observing the timer frequency, I enabled one of the pin as GPIO (PC7) and wrote toggling function in a while loop. When I measure from the oscilloscope, I see 1.642MHz signal. Does anyone have an idea how I can get 16MHz signal?Thank you

author: Yansi
author_rank: Super Contributor
timestamp: October 04, 2019, 08:58:33 pm
content: Because even thinking about toggling a pin in an interrupt with a 16MHz rate is completely brain-dead.

author: Yansi
author_rank: Super Contributor
timestamp: October 04, 2019, 09:02:24 pm
content: If you that much need to spew out 16MHz signal or what, use the dedicated MCO clock output, or jeeeez... at least put the timer in a suitable mode where the pin gets toggled via HW.Now study up about interrupts and their handling, then about the amount of bullshit within the Cube/HAL libraries to understand, why this is not possible.Clue: to get 16MHz output frequency, the toggle rate is 32MHz, so the CPU has 216/32=6.75 clock cycles to do the toggle work for you. And then it needs to do the other things too. Now you see what you did there?//EDIT: Note that to output 16MHz exactly, you would have to run the MCU from a 208MHz instead of 216. The numbers are not integer multiples.

author: newbrain
author_rank: Super Contributor
timestamp: October 04, 2019, 09:42:17 pm
content: Quote from: A.Ersoz on October 04, 2019, 07:38:09 pmWhat Yansi said...but let's expand a bit.If you set the period to 1-1=0, the timer will not count.The period is written in TIMx_ARR (Auto Reload Register) and the reference manual states:The counter is blocked while the auto-reload value is null.If the clock frequency is set to 108 MHz, it is impossible to get exactly 16 MHzNot even interrupts are mentioned, what has a while loop toggling a GPIO pin to do with the timer set up?How did you expect it to work?Now, some hints towards the solutions (no code, I've already exceeded my quota for today):The prescaler, as opposed to the ARR can be set to 0 (i.e. divide by 1), do you see how this can help you?The TIMx_CK can be reach HCLK speedalso for APB1, check the various options and flags in RCC.The closest multiple of 16 is 208 (16x13) or, if you are into overclocking, 224 (16x14)This is the most important part: as Yansi pointedly pointed out, forget using SW to toggle your pin.They also indicated a way to have an output square wave without even using timers (though it might get complicated obtaining 16MHz).But you wanted to use a timer, and that's a good choice.Which of the timer functions can Possibly Work Miracles for your problem?There are other ways, though, without using the above suggested solution: read the timers' Functional Description chapters in the reference manual to understand how to tie an output to a timer related event.All of the above can be easily set up in CubeMX, if you don't feel like fiddling with registers.