Electronic – Understanding Timers, Counters and Prescaler Registers on the STM32

armcortex-m3interruptsstm32timer

So I'm trying to set up a very simple periodic interrupt on my STM32VL-Discovery board using the libopencm3 libraries on linux. I've looked at the documentation and I just want to verify with somebody that I understand what's going on.

My understanding is that the prescaler register scales the internal clock to another frequency/clock that the timer actually sees. i.e. you set the timer prescaler register to 24Mhz/DesiredHz to get the timer to operate at the desired frequency. Is this correct?

Assuming that's correct, say I want to generate an interrupt on every timer clock cycle. I didn't see anything in the docs that would allow me to do that besides setting the period register to 1. This would generate an overflow interrupt every clock correct?

Best Answer

If you just set the period register to 1, this won't automatically enable interrupt generation. You will need to do 2 additional steps:

  1. Enable the 'update' interrupt for your timer using the TIM_ITConfig() function.
  2. Enable the timer interrupt source in the interrupt controller by calling NVIC_Init() with appropriate parameters.

Of course, you should not forget to enable clocking of your timer peripheral using the APB1PeriphClockCmd().

There's a detailed example on configuring timers on STM32F4-Discovery here: http://visualgdb.com/tutorials/arm/stm32/timers/

STM32VL-Discovery is a bit different, however the main steps are the same.