Professional Documents
Culture Documents
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 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.
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!
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.
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.
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 !
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 ?
This time we need to calculate how many ticks we should tell the MCU to produce 5 seconds
delay. Here is how it works :
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
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 1
Timer 2