EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: A.Ersoz on October 04, 2019, 07:38:09 pm

Title: Setting TIMER as 16MHz [STM32]
Post by: A.Ersoz on October 04, 2019, 07:38:09 pm
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
Title: Re: Setting TIMER as 16MHz [STM32]
Post by: Yansi on October 04, 2019, 08:58:33 pm
Because even thinking about toggling a pin in an interrupt with a 16MHz rate is completely brain-dead.
Title: Re: Setting TIMER as 16MHz [STM32]
Post by: Yansi on October 04, 2019, 09:02:24 pm
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.
Title: Re: Setting TIMER as 16MHz [STM32]
Post by: newbrain on October 04, 2019, 09:42:17 pm
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
What Yansi said...but let's expand a bit.

Now, some hints towards the solutions (no code, I've already exceeded my quota for today :=\):

All of the above can be easily set up in CubeMX, if you don't feel like fiddling with registers.