STM32 Timers 5.hafta
STM32 Timers 5.hafta
Timers
I will now discuss how to configure a STM timer using the HAL framework.
The discussion on the demonstration project presented below will show you
how to configure a GP timer in a polled mode, which in turn will blink an LED.
Selecting the clock source also triggers the CubeMX application to include all
the TIM2 initialization statements to be included in the new project. This step is
vital to generate a working project.
Don’t forget that all the other previous project creation steps are still
appropriate, such as including only the necessary library files and ensuring that a
hex file is generated.
I will discuss the key parts of the project code after the below listing.
Most of this code is a repeat of the main.c code shown in the interrupt
demonstration project discussed in the previous chapter. The key differences are
the addition of a MX_GPIO_Init method and some new operational code required
in the forever loop portion of the main method.
The MX_GPIO_Init method configures the TIM_HandleTypeDef, a typed C
struct with specific values to have the timer reset every 0.5 s. The struct is
named htim2 and is both initialized and started in this method.
The forever loop in the main method has code, which continually checks the
timer count and will turn on the LED when the count reaches 150. It will remain
on until the count progresses to a value of 399, at which point the LED will be
reset. These statements result in the LED being on for 0.25 s and off for 0.25 s.
Test Run
I built the project and uploaded it into the Nucleo-64 board. The user LED
immediately began to blink at a 0.5-s rate, which confirmed the program worked
as desired. The next step in this timer demonstration series is to implement an
interrupt-driven version that will also blink the LED, but not require any
specialized code in the main method’s forever loop.
I will not repeat the extensive code listing previously shown, but will instead
just present the code segments needed to implement the interrupt-based code.
The first part is the easy one, where the forever loop is changed to a null
action:
while(1);
The only other change that needs to be done is to set the timer period to 250
to ensure the LED blinks twice a second. The timer will now interrupt at the
appropriate interval without any special code needed to monitor real-time
counter values.
The following three statements must be added just before the forever loop in
the main method:
The TIM2 IRQ handler method must be defined in the main.c file as follows:
Finally, a callback method must also be defined, which toggles the LED pin
every time the TIM2 timer generates an interrupt:
Test Run
I made the modifications to the main.c file, built the project and then uploaded it
into the Nucleo-64 board. The user LED immediately began to blink at a 0.5
second rate, which confirmed the program worked as desired.
The program functioned just about the same with this modified code, which
used the modulus operator along with a toggle method to control the LED. The
code is much smaller than the original, but it is more complex and harder to
understand and consequently maintain. This change highlights an interesting
challenge that developers constantly face. This challenge is whether or not to use
simpler code, which can take more code space versus using compact and
“efficient” code, which is likely harder to understand and modify.
Test Run
I built the modified project and then uploaded it into the Nucleo-64 board. The
user LED immediately began to blink at a 0.5-s rate for 4 s and then at a 1.0-s
rate for 4 s, which confirmed the modified program worked the same as the
original program. I did notice that the LED seemed to flicker more using this
version vice the original. I can only attribute the flicker to the use of the toggle
method versus the firm set and reset methods.
This last demonstration completes all discussions and demonstrations that I
wished to convey regarding interrupts and timers. This chapter really is just the
tip of the proverbial iceberg because there is a tremendous amount of
information on these topics that I could not cover. There are almost 2,000 pages
alone in STM’s HAL user manual. As always, the STM datasheets, and user and
reference manuals will be an invaluable resource to learn about the many
additional features and functions that you can carry out with interrupts and
timers.
Summary
I started the chapter discussions with the focus on timers, which are important
peripherals because they alleviate the MCU from having to directly perform
timing functions. I explained the five available categories of STM timers, of
which there are three types provided on the project MCU. These are basic,
general purpose (GP), and advanced.
I next showed you how to configure a timer to perform a specific timing
event. This discussion was followed by a demonstration in which the onboard
LED blinked once per second. This demonstration did not use an interrupt,
which was the focus of the second timer demonstration.
In the second timer demonstration, I showed how easy it was to integrate a
timing operation with an interrupt process. In this demonstration also the
onboard LED blinked, but it blinked using only the interrupt process.
The third timer demonstration showed how to restructure the interrupt process
such that a multi-rate LED blink could be implemented. The interrupt callback
method controlled a counter variable, which in turn controlled the actual LED
activation within the forever loop.
In the final timer demonstration, I changed the original forever loop code with
a much more compact and complex version with the same functionality . I did
this to illustrate how developers must constantly decide on how to write code,
simpler and consuming more code space or very compact and complex, which is
harder to understand.