Discuss this article in the SERVO Magazine forums at http://forum.servomagazine.com
Our resident expert on all things
robotic is merely an email away.
roboto@servomagazine.com
Tap into the sum of allhumanknowledge and get your questions answered here!
From software algorithms to material selection, Mr. Roboto strives to meet you
where you are — and what more would you expect from a complex service droid?
by
Dennis Clark
This month’s column is dedicated to software issues
that many of you are struggling with, attempting to go
around, or are just plain avoiding by watching another
edition of The Daily Show, rather than wrestling a problem
to the ground and pinning it.
So, this time I’m going to answer these software
questions that I’ve been queuing up for just such an
occasion. I’ve chosen timers and Interrupt Service Routines
(ISR) for my topics, using as many compilers as I can on the
two most commonly used platforms I get questions about:
the Atmel ATmega and PIC series chips. Contrary to
popular belief, Mr. Roboto has not used every compiler or
chip extensively, just some here and there. Rather than
repeat every question and answer, I’ll break the questions
down into two categories and go over the nuts and bolts
of how to solve the problems.
You will have to bear with me here. Working with
timers and interrupts is rather like tying your shoelaces.
Once you are “in the know,” the process seems simple and
is automatic, but explaining just exactly how and why one
is doing each step is very laborious. So, I will start at “level
0” and work forward. I hope that you will then understand
and be able to apply the process to your own project, and
not just have to copy the code you find in these pages.
Q. So, let’s get started with our first generically posed question. How do I set up a timer?
14 SERVO 12.2011
A. I’ll start with the always popular ATmega168 or ATmega328 parts. These are commonly used on a variety of popular robot controllers like the Pololu
Orangutan and the Arduino series. I use them pretty often
myself. So, how do you set up a timer? Let’s take a look. I
promise, it won’t hurt and after you do it a few times, it
will be automatic to you.
The Compiler
I’m going to use the avr-gcc as my compiler of choice.
It is a good compiler, runs on Windows, Mac OSX, and
Linux, and it’s free. (What more could you want?) You
don’t have that? Well, go back through your SERVO
Magazine archives and find where I detail how to set it up,
either using Winavr or Eclipse (my choice) in the August ‘08
article.
Homework
I am not going to reproduce the entire 376 page
document Atmel provides for us to configure and use the
ATmega168/328 parts. You should get a copy of it from
your favorite parts supplier or Atmel ( www.Atmel.com) to
use as a reference while you read this article. Another good
place to get the ATmega168 datasheet is your parts
distributor; it will be easier to find there. I use Digi-Key
( www.digikey.com). Simply search for the ATmega168
and click on the PDF symbol where the part is described.
The Timers
There are three timers in the ATmega168/328 parts:
TMR0 and TMR2 (eight-bit), and TMR2 (16-bit). All of these
timers can do PWM, or they can be configured to just be
timers. TMR2 can also be configured as an Input Capture,
but that is another story. I’ll look at TMR2 for this
discussion.
While all of the timers can take either an I/O pin input
or use the system clock to advance the timer, TMR2 can
also use an external crystal, so this one can be used with a
32.768 KHz source to run a real time clock. We’re not
going there this time. We’ll select the system clock, which
in the case of an Arduino is typically 16 MHz. So, I’ll just
use 16 MHz as our clock source. If you use another system
frequency, substitute that frequency in when doing the
timing calculations that I’ll show you.