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.
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?
It’s 2012, a new year, with more to learn and more
experiments to try. What new wonders will be made
available by what seems to be an ever-expanding industry
of robotics components suppliers and creators? I’m sure
that I don’t know, but I’m just as sure I’m going to try to
find out! I hope to hear from those of you who try
experimenting with these new wonders and come up with
questions about new ways to apply them.
This month, I’m going to finish up my generic discussion
of timers and what we can do with them. The topic will be
the PIC microcontroller and its various ways to deal with
timers. Each PIC family uses timers; as the PIC has matured,
the timer subsystems have changed a little and become
simpler and easier to use, in my opinion. Older PIC16C parts
made you set a period register, then set your match number,
THEN set your counter. Most folks found that very confusing.
Newer 16F parts just have a timer pre-scaler setting and
the timer counter register (two registers for the 16-bit
timer/counters). I’m going to start with the venerable
PIC16F series, move to the PIC18F series, and end up with
the PIC24 series. You should see the similarities between
the timer modules of these very different PIC series variants.
Before I start though, I’m going to stand on my soapbox
for a minute. I still from folks wanting advice with programming
16F<something> parts as a robot controller. My advice to
them is “You need to move on!” For all but the simplest
robot projects, these parts just won’t provide enough “bang
for the buck” for you! Even the PIC18F parts should be
passed over for the newer PIC24 or even PIC32 devices. These
new 16-bit parts are under $4 usually and the 32-bit ones
are typically under $7. Microchip has free compilers and a
new IDE that runs under Windows, Mac, and Linux operating
systems. There isn’t any reason not to step up! ‘Nuff said.
I’m going to choose a C compiler for each of the PIC
variants rather than Assembly. While Assembly is fun and
all, it isn’t the easiest way for newcomers to the craft to
use an embedded processor like the PIC. For the PIC16
series, I’ll use the CCS PCM compiler. It is a low cost
compiler that will integrate with the MPLAB PIC IDE. For
the PIC18F series, I’ll use the Microchip C18 compiler which
can be used by the hobbyist or student for free. For the
PIC24F series, I’ll use the C24 (also called C30) compiler
which is also free for hobbyists and students.
Timers for the PIC16/Fxxxx
Figure 1. PIC16F TIMER1 layout.
PIC16F parts typically top out at 20 MHz. The clock to
the timers is then Fosc/4, which means when using a 20
MHz crystal your input clock to the timer is 5 MHz.
Remember that when you are setting the prescaler to
get the timing you want. Figure 1 shows the hardware
layout for the TIMER1 module in a PIC16F630; a very
useful 16F series PIC. (All datasheet graphics used by
permission of Microchip, Inc.)
The easiest way to use a timer is to set an interrupt
on the overflow of the timer’s counter register. The
timer register will overflow when the register
increments from its maximum value (0xFF for an eight-bit timer; 0xFFFF for a 16-bit; and so on) to zero, where
it starts counting all over again. To set the interval of
that interrupt, you follow this procedure:
1. Choose your interval (1 ms, 10 ms, whatever).
14 SERVO 01.2012