Off the back of their basic counter functionality, timers
generally offer a range of features that actually do make
We’ve dealt with prescalers in the past; they are like a
gearbox for the microcontroller’s clock, ratcheting down a
fast clock into speeds that are more manageable. A 16
MHz clock ticks 16 million times a second, resulting in a
time-slice that is far too small to be practical for most
applications. A 1024 prescaler slows that down by a factor
of (you guessed it) 1024, resulting in an achieved
frequency of 15. 6 kHz — far more useful (although still at
a pretty high resolution). We’ll be using the prescaler at its
maximum value for this article.
Yes, interrupts come into play once again as an
important tool. Timers are usually able to generate a
number of different types of interrupts; the two most
common being an overflow and compare interrupt. Let’s
look at the overflow first, as this is the simplest.
Remember how the current value of the timer is
stored in a register and that the registers we’ve dealt with
so far have been eight-bit registers that store a value from
0 to 255? So, what happens when the counter reaches
255? As with most registers (and variables in many
languages), it “overflows” back to a value of zero and
starts counting up again. You can configure the timer so
that it generates an interrupt each time this overflow
The overflow interrupt is great, but only if you want to
measure time in multiples of 1/61 of a second (that’s how
long it takes the timer to count up to 255 off a 16 MHz
clock). What if you want to measure a different value? The
answer is that you can set the timer to generate an
interrupt at any value between 0 and 255 by setting a
value in a compare register (on the ATmega328P, this is
called an Output Compare register). If you set a value of
127 into this register, an interrupt will fire every time the
timer count reaches 127.
Now, we’ve gained a bit of control and are actually
Controlling a Pin
In addition to generating an interrupt, many timers
can directly change the level of an output pin. This can be
really useful if you want, for example, to have an LED flash
away without any interruption or variation. Without
putting any strain at all on your microcontroller, you can
simply “set and forget” and the timer will make it happen.
Configure the registers and away you go. On the
ATmega328P, there are only certain pins that this can
happen on; those marked OC0A, OC1A, OC2A, OC0B,
OC1B, and OC2B.
Timing an Event
Not only can the timer affect a change on a pin, it
can also be used to detect a change on a pin. When a
change on a pin occurs, the value of the timer is stored in
a “capture” register, and an interrupt is generated to let
you know that there’s a value waiting to be processed.
You could think of this functionality as a stopwatch for
microcontrollers — useful for timing very small events; for
example, measuring the frequency or time-based levels of
an external component (ever wanted to build a basic
Generating PWM Signals
Timers are also able to generate PWM (pulse-width
modulation) signals; in fact, this is how many
microcontrollers implement PWM functionality. In the
world of Arduino, we used the analog Write() function to
achieve this. Behind the scenes, however, it was all being
run by the timers.
It is a little more complex in the “raw” world of AVR
microcontrollers, but an understanding of the concepts
will allow you to work with a much broader range of
microcontrollers than the Arduino ecosystem allows.
By Andrew Retallack Post comments on this article and find any associated files and/or downloads
at www.nutsvolts.com/index.php?/magazine/article/november2015_Retallack. www.crash-bang.com
November 2015 53
Time to Talk Frequency
When I first started exploring microcontrollers in general
and timers in particular, I was taken back to my high school
days as I began to delve into frequency and period. Perhaps a
refresher will help you in the same way it did me.
Frequency is the number of times an event occurs in a
specific time period. In the world of computing, we measure
frequency in Hertz (Hz), which refers to the number of events
per second. Our microcontroller runs at 16 MHz, meaning that
its clock ticks 16 million times in a second. In our LED
projects, we typically blink the LED once a second; in other
words, at a frequency of 1 Hz.
Period (or the time taken for one clock tick to complete)
is the reciprocal of frequency. If you divide “1” by a
frequency, the result given is a duration in seconds; 16 MHz
results in a period of 62. 5 nanoseconds, or 0.0000000625
seconds. These are pretty short slivers of time!
A clock prescaler divides the frequency of a clock
source (e.g., the main clock of your microcontroller) by a
specific number. So, the 1024 prescaler will generate a “tick”
for every 1024 cycles of the main clock. The result is that the
frequency decreases to 15.625 kHz ( 16,000,000 / 1024).
Remember that period is the inverse of frequency, so the
decrease in frequency results in an increase in period. We
now have a period of 64 microseconds (or 0.000064 seconds).