S. Marsanne

STM32 Timers Overview

Discussion created by S. Marsanne Employee on Dec 23, 2016

For new to STM32, Timers may look overwhelming as their featureset grew up over the years.

Let's walk around the Timer basics step by step. This won't replace the datasheet nor application notes and is aimed at newcomers.

 

Let's first look at the STM8 timer:

 

======= TIMER CLOCK SOURCE ======================

 

Timer need a clock source, usually there are 2 sources:

- Use an internal clock source, typically derived from the core frequency

- Use an external clock source, typically coming from ETR pin as alternate function

==> Let's imagine the clock source is 16 MHz

 

CK_PSC is a clock prescaler which divides the clock source by a number.

==> If CK_PSC is 15, the resulting timer reference clock would be 1MHz

 

======== BASIC TIMER OPERATION ====================

 

What timers do? Timers have counters, typically 16 bit, and count up typically.

They can stop or overflow from a value programmed in AutoReload Register.

==> This will be the timer period. If the AutoReload = 1000, the period would be 1 msec

 

Timers can run and overflow continuously, or run in one shot mode. Some timers have a Repetition Counter to let the timer overflow a fixed number of time: It's a mode between one pulse mode and continuous mode.

 

When the timer overflow, a flag is set and can trigger an interrupt on demand, creating a timebase.

 

=== INPUT CAPTURES ==================================

 

Input Capture means taking a timer snapshot based on input pin event.

Typical event can be a rise and/or falling edge.

Say we capture falling edges, if the first capture is 100 and the next capture is 500 (using interrupt to save values), the period would be 400 usec. What happen if a timer overflow occurs between the two edges? No problem you may get 900 and 300 : The overflow is detected by second value lower than first and compensate for it by adding the overflow to the second value: 300+1000-900 = 400

 

When an input capture event occurs, typically an interrupt will save the snapshot and release the pending interrupt.

If the capture is triggered on ANY edge, and the rise and fall captures are recorded, it is possible to measure both the period and duty cycle of an incoming signal.

 

Additional features for demanding pool of application is to be able to filter the incoming signal, for example a digital or analog filter to filter glitches, and prescalers if the incoming signal is too high frequency.

 

========= OUTPUT COMPARES ===================================

 

Output compare is to affect an output pin based on a pre-programmed timer matching value.

The behaviour of the output compare pin is usually programmable, output a preset bit, toggle, etc...

If the timer is free-running and the output compare register is programmed to make falling edge at compare value 700, it will generate a 70% duty cycle PWM at 1 kHz frequency. (with 1000 steps for the duty cycle)

 

========== OTHER SPECIFICS ===================================

 

In some advanced timers, 3 output compares have complemented outputs. This is mostly for motor control applications.

 

Now, let's have a look at an STM32 Timer block diagram:

 

There are 3 type of Timers, 8 bit, 16 bit, 32 bit. Some timers have 0 to 4 capture/compares.

CH1 and CH2 can also be used as clock or trigger source.

DMA is introduced so it is possible to either automatically save input capture values in RAM, or generate dynamically changing output compares (for example cyclically generate a PWM pulse pattern to generate a sine wave using an external RC low pass filter).

 

Also, as STM32 has lots of timers, it is possible to cascade Timers to make more complex functions. 

 

Timers can also generate an internal trigger event to chain it to other peripherals or other timers.

 

During debug, chain the timers using GPIOs so it is possible to probe with an oscilloscope.

 

Hope this helps browse through all the rich features of STM32 Timers.

 

Questions? Suggestions? Helpful?

Outcomes