Timers STM32
Timers STM32
Let’s consider a basic 16-Bit timer like the one shown below. As a 16-Bit time, it
can count from 0 up to 65535. Every clock cycle, the value of the timer is
incremented by 1. And as you can see, the Fsys is not the frequency that is
incrementing the timer module. But it gets divided by the Prescaler, then it gets
fed to the timer.
Basically, in timer mode, the TCNT register is incremented by 1 each clock cycle
@ the following frequency (Fsys/PSC). This means if the Fsys is 80MHz & PSC
is 1:1024, the TCNT gets incremented by 1 every 12.8μSec. Therefore, if you
start this timer to count from 0 until it reaches overflow (at 65535), it will give you
an interrupt signal once every 0.839 Second.
What if I need to set up this timer to give me an interrupt signal once per 1
second? I don’t want this 0.839Sec time interval in fact. Well, for this reason,
there exists a possible hardware feature called preload register that forces the
timer to count from any arbitrarily chosen value up to the overflow. So, you no
longer have to start counting from zero. Hence, any time interval can be obtained
with a timer module.
A timer module can also operate in a counter mode where the clock source is not
known, it’s actually an external signal. Maybe from a push button, so the counter
gets incremented every rising or falling edge from the button press. This mode
can be advantageous in numerous applications as we’ll discuss hereafter. But for
now, consider the following diagram.
Where you can see, the clock signal is now driven from the push button and gets
to the timer clock input through the Prescaler. And you can capture the
information of how many times the button is pressed by simply reading the TCNT
register’s value.
In counter mode, the counter counts from 0 to the auto-reload value (contents of
the TIMx_ARR register), then restarts from 0 and generates a counter overflow
event. An update event can be generated at each counter overflow or by setting
the UG bit in the TIMx_EGR register (by software or by using the slave mode
controller).
The LPTIM is a 16-bit timer that benefits from the ultimate developments in power
consumption reduction. Thanks to its diversity of clock sources, the LPTIM is able
to keep running in all power modes except for Standby mode. Given its capability
to run even with no internal clock source, the LPTIM can be used as a “Pulse
Counter” which can be useful in some applications.
Also, the LPTIM capability to wake up the system from low-power modes makes
it suitable to realize “Timeout functions” with extremely low power consumption.
The LPTIM introduces a flexible clock scheme that provides the needed
functionalities and performance while minimizing power consumption.
The Low-Power Timers (LPTIM) Main Features:
16-bit up-counter
3-bit Prescaler with 8 possible dividing factors (1,2,4,8,16,32,64,128)
Selectable clock
– Internal clock sources: LSE, LSI, HSI16 or APB clock
– External clock source over LPTIM input (working with no LP oscillator
running, used by Pulse Counter application)
16 bit ARR auto-reload register
16 bit compare register
Continuous/One-shot mode
Selectable software/hardware input trigger
Programmable Digital Glitch filter
Configurable output: Pulse, PWM
Configurable I/O polarity
Encoder mode
Repetition counter
2.3 General-Purpose Timers Modules
For control and monitoring purposes, the timer has also timing measure
capabilities and links to built-in ADC and DAC converters. Last, it features a light-
load management mode and is able to handle various fault schemes for safe
shut-down purposes.
High-Resolution Timers (HRTIM) Features Include:
In this section, we’ll get just a brief description of each mode of operation. How it
works and what sort of applications it fits for. Each mode will be discussed in-
depth with practical LAB experiments in future tutorials. But for today, we’ll get
into Timer Mode in section4 and do its LAB in the next tutorial. Other modes will
be coming later on in this series of tutorials.
In timer mode, the timer module gets clocked from an internal clock source with a
known frequency. Hence the clocking frequency is known, the overflow time can
also be calculated and controlled by the preload register to get any arbitrarily
chosen time interval. Each timer overflow, the timer signals the CPU with an
interrupt that indicates the end of the specified time interval.
This mode of operation is usually used to get a specific operation done each
specific time interval. And to achieve timing & sync between various tasks and
events in the system. It can also replace delays in various situations for better
system response.
3.2 Counter Mode
In counter mode, the timer module gets clocked from an external source (timer
input pin). So the timer counts up or down on each rising or falling edge of the
external input. This mode is really helpful in numerous situations when you need
to implement a digital counter without polling input pins or periodically reading a
GPIO or continuously interrupt the CPU if you’ve chosen to hook it up to an EXTI
pin.
You can actually monitor the counter value difference each time interval to tell
how many pulses did occur or what was the frequency of it. Such a mode can be
advantageous in many situations like this. And more examples are to come in
upcoming tutorials.
In PWM mode, the timer module is clocked from an internal clock source and
produces a digital waveform on the output channel pin called the PWM signal. By
using output compare registers (OCR), the incrementing timer’s register value is
constantly compared against this OCR register. When a match occurs the output
pin state is flipped until the end of the period and the whole process is repeated.
The timer in PWM mode will produce a PWM signal at the specified frequency the
user chose. The duty cycle is also programmatically controlled by its register. The
PWM resolution is affected by the desired FPWM and other factors as we’ll see in
the dedicated tutorials for PWM generation.
3.4 Advanced PWM Mode
The advanced PWM signal generation refers to the hardware ability to control
more parameters and add some hardware circuitry to support extra features for
the PWM signal generation. Which includes:
Here is an example for a counting timer in output compare mode. Note when the
output state is changed (toggled) and the value in the OCR (compare register
TIM1_CCR1).
One-pulse mode (OPM) is a particular case of the previous modes. It allows the
counter to be started in response to a stimulus and to generate a pulse with a
programmable length after a programmable delay. Starting the counter can be
controlled through the slave mode controller. Generating the waveform can be
done in output compare mode or PWM mode.
A pulse can be correctly generated only if the compare value is different from the
counter initial value. Before starting (when the timer is waiting for the trigger), the
configuration must be CNT<CCRx ≤ ARR (in particular, 0<CCRx). For example,
you may want to generate a positive pulse on OC1 with a length of tPULSE and after
a delay of tDELAY as soon as a positive edge is detected on the TI2 input pin.
3.7 Input Capture Mode
In the encoder interface mode, the timer module operates as a digital counter
with two inputs. The counter is clocked by each valid transition on both input pins.
The sequence of transitions of the two inputs is evaluated and generates count
pulses as well as the direction signal. Depending on the sequence the counter
counts up or down. So you don’t have to detect these pulses individually and see
which came first to detect rotation direction and this kind of work. Now, all of this
is done by hardware thanks to the encoder mode hardware support.
In timer gated mode, a timer module is also said to be working in “slave mode”.
Where it only counts as long as an external input pin is held high or low. This
input pin is said to be the timer gate that allows the timer to count or not at all.
This mode can be used in a wide range of applications and signal measurements.
It can help you measure extremely short pulses with a very high resolution. And
also trigger the timer to count on external events from sensors or other MCUs.
The counter starts counting on the internal clock as long as TI1 is low and stops
as soon as TI1 becomes high. The TIF flag in the TIMx_SR register is set both
when the counter starts or stops. The delay between the rising edge on TI1 and
the actual stop of the counter is due to the resynchronization circuit on TI1 input.
The STM32 timers, not all of them, have the capability to generate multiple DMA
requests upon a single event. The main purpose is to be able to re-program part
of the timer multiple times without software overhead, but it can also be used to
read several registers in a row, at regular intervals.
An infrared interface (IRTIM) for remote control can be used with an infrared LED
to perform remote control functions. It uses internal connections with TIM15 and
TIM16 as shown in the diagram down below. To generate the infrared remote
control signals, the IR interface must be enabled and TIM15 channel 1
(TIM15_OC1) and TIM16 channel 1 (TIM16_OC1) must be properly configured
to generate correct waveforms. The infrared receiver can be implemented easily
through a basic input capture mode.