This presentation gives you an anusual application note of STM32 timers. This application note makes it possible to process signals efficiently and without being obliged to use the CPU.
1. What isn’t told about Timers in
STM32 application notes!
« Signal Processing as an example »
By Omar Bouzourraa
August 2017
2. The main goal ??
In this brief presentation, I’m going to show you something
amazing about STM32 microcontrollers!
My objective is implementing an ideal filter using only stm32
microcontroller and with a full hardware solution. In other
words, my goal is :
- Not to use external hardware components
- not to use STM32 Microcontroller CPU
- obtain an ideal output from the filter
3. What’s amazing in all of that?
STM32 Timers are always used for timing puposes, which include signal
measurement and generation.
In this presentation, you will discover that STM32 Timers can be used as
blocs of digital comparators.
Timers with 4 channels can be used as 4 digital comparators. Signals
that describe comparaison result, are obtained on Timer output
channels.
The capture compare registers are used to preload constant reference
datas which are useful for comparaison.
The data to be compared with the capture compare registers, is
stocked in the Counter Register and can change (without CPU
intervention) while program is running.
5. Let’s explain it more!
• CCR or capture compare register, is a timer related register. It’s used for
stocking data useful for setting the pwm duty-cycle when timer mode is
PWM Output. For further information about this mode and other modes,
please check STM32 reference manual.
• CNT or Counter is also a timer related register. It usually contains the
digital counter value. Please don’t confuse between the counter and the
register that contains the counter value. In our case, counter should be
disabled and CNT is referred to as the register and not the counter!
• ARR or Auto Reload Register, is a timer related register. It contains a
constant value which is known as the signal period (when typically
working in PWM output mode). When the counter value reaches the ARR
value, it is then reinitialized (the counter value CNT is reinitialized to zero
when up-counting mode is enabled). In this presentation, we will use the
up-counting mode. There is also down-counting and up-down counting
modes.
6. The PWM signal generation mechanism
Now, I will explain how typically a timer works when PWM output mode is
enabled. I suppose that we have enabled the up-counting mode and the PWM
output mode PWM Mode1 (please refer to the reference manual for further
information about PWM mode1 and PWM mode2) and the output polarity is
not inverted.
If the counter is enabled in the bit 0 of CR1 rgister :
the Timer output is active high (at the beginning).
the counter is incremented every sensitive edge detected on the timer
source clock.
ARR and CCRx (x can be a value from 1 to 4) are constant while they are not
modified by the CPU.
Always, CNT and CCRx are compared due to a digital comparator embedded
with each channel.
When CNT > CCRx, an event is generated and the Timer output state is low.
When CNT = ARR, a second event is generated and the counter value is
reinitialized to zero and so on …
8. The STM32 Timer as a digital comparator
Now the process is very simple! Just we need to configure our
timer exactly the same way when we want to generate a
PWM signal. But we must enable output without enabling the
counter. The counter must be disabled in order to disable its
automatic evolution.
Let’s forget the counter and focus only on the CNT register. In
fact, we don’t really need the counter but we need its value!
Good news! The counter value is modifiable on the fy.Just
write a value in the CCRx register (it is the reference value for
comparaison).
9. How can you modify the CNT value
without using the CPU ?
Now, it’s the time to talk about the magic of the DMA controller!
DMAC : Direct Memory Access Controller. For more information, please
check the reference manual!
I will show you how you can measure a PWM signal frequency using a
classic configuration of an STM32 Timer in PWM Input mode. This
measure is transferred to the CNT of another Timer (in the same
microcontroller !) using the DMAC .Finally, data is transferred to the
CNT without a CPU intervetion.
As a conclusion: comparing the period of a signal with a reference
period value and generating the comparaison result without being
obliged to use the CPU has huge advantages : We can avoid software
latency and deal with signals with high frequencies , efficiently and use
CPU for further applications at the same time!
10. How can this be useful in signal
processing ?
As I said at the beginning of this presentation, my goal is to
build an ideal filter using only STM32 microcontroller.
Let’s talk a bit about low-pass filter!
Low-pass filter is typically used for elimination of frequencies
higher than a reference frequency which is known as cut-off
frequency. But the problem is that obtaining a precise
response demands a complex hardware architecture and
always, an ideal response is so far to get!
Low-pass filter is just an example! You can design all the other
filters you know, using the exact same logic.
11. Low-pass filter implementation with stm32
timers
I suppose we want to build a low-pass filter with a cut-off frequency
F0 = 20Khz. The Timer source clock for signal measurement is
F_Source = 168 MHz. For a measurement precision, the prescaler must be kept in at
zero value. The ARR must be initialized to its maximum value.
Using the famous formula given in the reference manual, we realize the value which be
latched on the CCR1 register of the timer used for measurement, is CCR1 = 8400 =
0x20D0.
As we are working on low-pass filter, the filter output must be zero as soon as the
measured frequency is higher than 20KHz else the output is an « identical » copy of the
measured signal.
Now, we choose a secod timer to work as a digital comparator. Using the DMAC, the
CCR1 register value of the measurement timer is copied in the second Timer CNT
register. In CCR1 register of the secod timer, you preload the value « 0x20D0 ».
13. Further Applications
• You can do the same thing as decribed in the low-pass filter, in order to design your
high-pass filter.
• For band-pass filter, there is something new, to highlight. The question in this case
is: How should we do, to combine 2 comparaisons (the measured frequency is
compared with two reference frequencies defined in CCR1 and CCR2 registers of
the timer configured as digital comparator (TIM3)) and generate only one signal to
control the transistor of the GPIO port ?
• We can simply use a logic AND between the two comparaisons to obtain the
desired control signal.
• And if we don’t want to add an external logic gate, we have a solution offered by
STM32 timers. We can use the Combined Mode.
• The combined mode is a specific feature (not available in all STM32 products)
available on STM32 Timers. It makes it possible to generate two PWM signals on
TIM_CHn and TIM_CHn+1 and make the logic « AND » or « OR » with the two
signals. Combined signal is then obtained on TIM_CHn. Please check the reference
manual to get more informations!
14. And What should I do if the signal is analog ?
• When we are dealing with analog signals, we can simply use
comparators and DACs available in STM32 products.
• Comparator is used to compare the analog signal with a
reference voltage, in order to generate internally a PWM
signal. This signal is connected internally to a Timer to
measure its frequency (You can use exactly the same
architecture described in the last slides to design your filter).
• DAC (Digital to Analog Comparator) is used to generate the
reference voltage.
• Don’t forget that all of this, is done in the same STM32
microcontroller!
15. And we can go further!
• I suppose that after obtaining the filtered
signal, I want to connect the output signal to
another hardware bloc. In this case, I can
disturb the source signal, so a follower
OPAMP is necessary.
• You are not obliged to use an external
OPAMP, because STM32 microcontrollers
offer a lot of them in only one chip. Check
your reference manual for more details.