# Introduction to Timers

The STM32 devices are built-in with various types of timers. In this series of tutorials it will be briefly explained some of their features and options as well as their general information and implementation.

STM32F4 comes with 14 built-in timers divided into 3 categories: General-purpose, advanced and basic timers. Each type of timer has its own special usage and characteristics but all of them share the same purpose of counting and telling the time.

It is a great feature because it allows us to do some really cool things like: time keeping and measurement, controlling angle of a servo shaft, receiving sensor data or as an interrupt source for tasks that need to be executed on a regular basis.

### Wasting time, 1st try

Let’s simplify communication between STM32 and some random temperature sensor. (Note: For simplicity reasons we won’t count the delays sensor needs to make for fetching the data from its registers). The sensor is in slave mode and can’t initiate interrupt so as a master STM32 needs to ask every now and then for the new data. How would you implement that?

The data is saved locally for future processing so interrupts need to be on an equal time distance. First step is to decide on what time interval are interrupts going to be. One of the possible solution is to have interrupts every one second but the problem of how to calculate a second still remains. First guess is to have “some” loop to waste time.

Good news is that code above works and it will waste some time before contacting the sensor again but the bad news is really really bad. Main problem in the code above is its inconsistency. The time processor spends in the function depends on current processor load. So hypothetically on the first call, function could waste 1.123 sec but on the second 0.95 and so on.

Example above is using processor time and resources with useless work while processor could do something else. The last thing is how could we even measure the time to count from 1 – 5000? There are built-in functions to do so but unfortunately they don’t work in this case. (Note: It can be done by reading the timer before function call and then again after function call. That time needs to be subtracted by the time timer uses for the read calls).

The answer for above delay implementation is in the timers!

### What timer is exactly?

The term timer/counter itself reflects the fact that the underlying counter hardware can usually be configured to count either regular clock pulses (making it a timer) or irregular event pulses (making it a counter).

STM32F4 can have processor clocked up to 180MHz (HCLK). Which means that STM32F4 can do 180 000 000 clock cycles per second. Under the hood of STM32F4 discovery is Cortex M4 processor with 225 DMIPS or in the same equivalency 1.25DMIPS/MHz (1.25 DMIPS per 1 MHz clock frequency).

DMIPS (Dhrystone MIPS ( Million Instructions per Second)) is a measure of computer performance relative to the performance of the DEC VAX 11/780 minicomputer of the 1970s.  (Note: check STM32 page for more details).  So basically 225 DMIPS describes the theoretical number of instructions the processor can handle in a second. The actual performance may be slower if your memory accesses can’t keep up, or faster if you can use processor features to bypass overhead.

Let’s do some math on number of instructions per second. What we want to do is divide one by clock cycles per second to get the time needed for one cycle.

$$\frac{1} {180 000 000} \simeq{5.5555} * 10^{-9}$$

Which means that one clock cycle approximately lasts for 5.55 ns. So if you want to measure five second delay STM32 needs to count up to:

$$\frac{1} {5.5555} * {5} \simeq{900} * 10^{6}$$

Luckily for us STM32 comes with two 32-bit up-counting timers so there is no problem counting up to $${900} * 10^{6}$$. The real problem comes when you want to make delay longer than the number timer can count clock cycles. In this case of 32-bit timer which can count up to $$2^{32} – 1$$ can produce 23.86 second maximum delay.

$$\frac{2^{32} – 1}{180 * 10^{6}} \simeq{24} sec$$

As you see 24 sec delay isn’t impressive so the microcontroller provides a very useful feature called prescaling. Prescaling is simply a way for the counter to skip a certain number of microcontrollers clock ticks. STM timers can have prescalers up to 16-bit(1 – 65536) values which means if prescaler is set as 1024, then the counter will count every time the clock ticks 1024 times. If we take above example, 32-bit timer with 1024 prescaler can now produce 24 433.59 second delay.

$$\frac{2^{32} – 1}{\frac{180 * 10^{6}}{1024}} \simeq{24 433} sec$$

STM made also possible to change timer counter period. Which would mean, if you have 32-bit timer you don’t have to count all the way up to $$2^{32} – 1$$. It makes things easier as it is possible to work with smaller numbers.

### Difference between SysTimer and Timers

You could be wandering why all the hassle when STM32 got in-built delay function? SysTimer also known as System Timer or simply SysTick comes preconfigured and easy to use. Which means there is no prescaler and counter period calculations nor thinking about clock cycles or how to calculate 350 ms if clock is on 20 Hz.

The SysTick calibration value is fixed to 18750, which gives a reference time base of 1 ms with the SysTick clock set to 18.75 MHz. It can be used to create time delays or generate periodic interrupts. So for precise 400 ms delay you can simply write.

The answer is in Real Time Operating Systems (RTOS). SysTick is a 24-bit down-counting timer used to provide a system tick for RTOS. Long put short if you aren’t using RTOS feel free to use SysTick as you please.

### Wrapping things up

As you saw timers are excellent feature which gives you time control superpowers. It is possible to count events, measure the pulse length of input signals or generating output waveforms. Most important parameters to begin with tweaking the timer would be:

• Prescaler: Allows longer delays, up to 16-bit
• Counter Mode: Can count up or down, compares value with value in Auto-reload register (Counter Period)
• Counter Period: Allows to shorten count periods

To give you headstart, for one second delay/interrupt set parameters as noted below to get 1 Hz period.

• F_CPU: 180 MHz
• Prescaler: 18000
• Counter Mode: Up
• Counter Period: 10000