You are on page 1of 4

TIMER

What is timer?

Timer is a clock inside your MCU! I think this is the easiest way to picture it. It handles kind of
tasks that requires time accuracy. Let’s say we want to blink a LED, we want it to light up for 2 seconds,
and goes off for 2 seconds. How do we communicate ‘2 seconds’ to MCU? Yes by using timer.

The application of timer can be seen anywhere in our daily lives, from alarm, microwave, rice
cooker, phone, phone, laptop, etc. In microcontroller, timer is also called counter, that’s why if you take
a look at the timer section in datasheet you will see it’s written as timer/counter. Let’s talk about how it
operates inside the MCU.

Timer/Counter inside MCU

Timer/Counter is basically a 8-bit or 16-bit register, the 8-bit register could count to maximum
value of 255, whereas the 16-bit could count to maximum value of 65535.

n-bit The calculation Max value


8-bit 28 - 1 255
16-bit 216 – 1 65535

Now you may be wondering ‘why do you need to substract one from the maximum value?’
Well, because the MCU counts everything in bits, and in bits we count from zero. Here I’ll explain it :

Let’s say we’ve got 4-bit register, which means this register can count from 0 up to 15 (24 – 1 =
15), but of course there are actually 16 (24) kinds of combinations!

4-bit value Hexadecimal value nth bits combination


0 0 0 0 0 1
0 0 0 1 1 2
0 0 1 0 2 3
0 0 1 1 3 4
0 1 0 0 4 5
0 1 0 1 5 6
0 1 1 0 6 7
0 1 1 1 7 8
1 0 0 0 8 9
1 0 0 1 9 10
1 0 1 0 10 11
1 0 1 1 11 12
1 1 0 0 12 13
1 1 0 1 13 14
1 1 1 0 14 15
1 1 1 1 15 16
TIMER

See what I mean? I also kinda having a hard time differentiating between those two the first
time I learn microcontroller, but hang in there, the more you work using bit, the more you’ll understand
and get used to it.

Now, let’s consider we’re working with 4-bit timer/counter register, when declared, the register
will change its initial value step by step until it reaches its maximum value. Tick, is the common word to
describe this increment or decrement process. Just like the clock ticks, the register also ticks.

0000

1st tick

0001

2nd tick
0010

3rd tick

0100

15th tick

1111

Notice that the number of ticks equal to the number of bit-values. The number of ticks is really
important since it will determined the value of register we write into our program to get the desired
delay time. We will talk about it later.

Now, are you wondering if the register is ticking, how fast does it tick? What is the time needed
to change from a value to the next one. Worry no more, let me introduce you to clock and crystal.

Clock and Crystal

Our microcontroller has a clock built-in inside, so that, it understand how fast it should tick.
Clock source can come from oscillator or crystal (XTAL). Let’s first talk about the most common type of
external clock, oscillator. Here is how the clock work :

1. The clock usually has its own frequency defined such as 8MHz. Here’s how it looks on top of
crystal.
TIMER

2. By knowing its frequency, we can easily obtain its period by the following formula :
1
T=
𝑓
1
T=
8000000
T = 0.125 μs
Where T = period, f = frequency

3. Now let’s remember what we’ve studied in highschool regarding the frequency and period,
here is a little illustration to help you.

Or we can recall what unit does each parameter has,

frequency = Hz (cycle/second)

period = second

Where in this case, the cycle equals to ticks. So yes, you guess it right!

4. This clock could produce 8M ticks/second, with each tick lasts 0.125 μs !

WOOO, that’s fast!!

Yeah, in human scale it’s fast enough, we can’t even see it in naked eyes, but there are many
faster clock out there even The Flash couldn’t see it.

Now we’ve got the period of frequency of the clock, how to translate this information to a
certain period of time that we actually want ?

Consider the following problem :

What bit is our timer/counter register? 8-bit

How long does our design need ? 5 ms delay

What clock frequency we have? 8MHz

How many ticks needed ? ??????

This time we need to calculate how many ticks we should tell the MCU to produce 5 seconds
delay. Here is how it works :

1. Determine the maximum value the timer/counter register


It is defined that our register 8-bit register, therefore the maximum value it could go is :
28 – 1 = 255

2. Determine the time required to produce one tick (period of clock)


TIMER

Since the frequency of clock in this problem is the same as the one discuess before, we already
know that the period of clokck is 0.125 μs

3. Determine the maximum delay could be produced in one clock cyle


Now imagine that our register could go from 0 – 255. This means there are 255 steps, and from
the period of the clock we know that each step takes 0.125 μs, so what is the maximum delay?

Maximum delay in one cycle = 255 × 0.125μs


Maximum delay in one cycle = 31.875 ≈ 32 μs
So it takes, 32 micro seconds for the register to tick from 0 to 255 (one cycle).

4. Determine the number of tick needed to get our desired delay


From all the data provided above, we could get the number of ticks to produce 5 seconds by
calculating :
𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑 𝑑𝑒𝑙𝑎𝑦
Ticks needed = −1
𝑀𝑎𝑥𝑖𝑚𝑢𝑚 𝑑𝑒𝑙𝑎𝑦 𝑖𝑛 𝑜𝑛𝑒 𝑐𝑦𝑐𝑙𝑒
5 𝑚𝑠
Ticks needed = −1
32 𝜇𝑠
Ticks needed = 155.25 ticks
Ticks needed ≈ 155 ticks

Sadly the built-in clock is not really accurate hence making it unreliable, therefore external one is much
preferable. There are various definition and configuration regarding clock, but first let’s understan

Timer 0

Timer 0 is a single channel 8-bit Timer/Counter

Timer 1

Timer 2

You might also like