Time Base Generation (Tim2) - STMicroelectronics STM8 Manual

Table of Contents

Advertisement

Time Base Generation (TIM2)

Time base generation is the most basic property of any timer and is also the most needed requirement
in embedded systems. This mode can be used with or without interrupt. We'll first check the method
firstly without interrupt and then with interrupt.
With time base generation, we can accurately time stuffs and events that are more precise than using
delays, loops or other methods. Time base generation utilizes hardware timers and so work
independently from other processes. It has many uses. For instance, with it we can avoid software
delays, generate time slots of a Real-Time Operating System (RTOS) and many other tasks.
The time base unit for all timers of STM8 is all same. There are a few differences. For example, Timer
1 (TIM1) has a repetition counter. It is like a counter within another counter. Other timers lack this
part. All timers can count up while advance timers can count down too.
The basic theory of time base generation is you have a peripheral clock which you would like to scale
according to your need. Thus, you prescale it and use the new clock to run a counter. The counter will
tick, incrementing count as time flies. It is just like counting from 0 to 100 and repeating from 0 again
after reaching 100. Shown below is the generalized formula for finding an important event called timer
reload:
(������������������ × �������������������� �������������� × ������������)
�������� ���������� (���������� ������������) =
��
������������
This is the amount of time that will pass before timer overflow event occurs and the timer restarts
from its initial count.
In my example, the peripheral or master clock is set to 2MHz. Thus, to make timer 2 (TIM2) reload
after roughly 2 seconds, I have to prescale it by a factor of 2048 and load it with 1952 counts. Note
that TIM2 doesn't have a repetition counter and so it is set to 1.
(2048 ×1952)
�������� ���������� (���������� ������������) =
6
2 × 10
= 1.998s
≈ 2s

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the STM8 and is the answer not in the manual?

Questions and answers

Table of Contents

Save PDF