T
S
I
S
E
N
G
T
T
A
P
L
N
R
A
I
E
O
H
G
T
R
T
R
T
T
A
M
E
E
M
I
D
G
N
G
W
M
I
I
C
T
R
O
H
C
O
N
T
R
P
O
L
I
L
E
R
S
C
s
■ BY CHUCK HELLEBUYCK
REAL TIME CLOCK
ONE OF THE MORE INTERESTING THINGS I’ve discovered about the
readers of Nuts&Volts is their diversity. Through the emails I’ve received
from writing this column, I’ve discovered that both hobbyists and
professionals read these articles.
Readers vary from complete
beginners to those with lots of
experience programming PICs, but
who still read to pick up some tips. It
makes it hard to write an article to suit
everybody, but I have learned that my
method of writing snippets of code
with a specific, simple purpose is
appreciated by all.
I try to follow that method here
and in all my books. This allows the
beginner to see how to accomplish
something and also allows the experienced PIC user to easily expand the
code for their particular application.
With that in mind, I want to
introduce using the PIC Timer1 —
driven from its own clock crystal — to
produce a simple clock (or as it’s
known in the microcontroller world, a
■ FIGURE 1
90
September 2006
“real time” clock.)
When you are talking about the
bit speed or processor speed or even
instruction cycle timing, it’s often
referencing the crystal or resonator
running the microcontroller. It will
be expressed in MIPS or Million
Instructions per Second and it refers
to the microcontroller’s internal clock.
This isn’t the time we humans use
to determine if we can go home from
work or if we should turn on the TV to
catch the final game of the Stanley
Cup (I’m a big hockey fan). Therefore,
this human clock is considered the
“real time” clock (RTC). There are
numerous RTC chips available and
some are RTCC or Real Time Clock
and Calendar chips and, in the future,
I’ll talk about those as well, but in
many applications, all you need is
time and if you can do it easily with
the PIC already in your application,
why spend the extra board space and
extra cost for a separate clock chip?
TIMER1
Inside many of the Microchip
PICs exists a 16 bit timer called
Timer1. One of the features of this
timer is the ability to run it from a
separate crystal than the rest of the
microcontroller. This means it can
actually run when the PIC is doing
NOTE:
■ The complete software listing is
available on the Nuts & Volts website
at www.nutsvolts.com
something else at a totally different
speed. It can also be set up to run
when the rest of the PIC is in low
power sleep mode. This offers many
advantages to your next PIC design
because now you can add an
accurate timebase even if you don’t
need a real time clock.
The Timer1 is 16 bits wide so it
can count from 0 to 65535. On the
65536th clock pulse, it rolls over (or
overflows) to zero again and sets an
interrupt bit (also referred to as a flag).
In the PIC16F876A I like to use, the
interrupt flag is the TMR1IF bit of the
PIR1 register. By monitoring this
bit, we can tell when Timer1 has
overflowed.
The PIC offers the option of
creating an interrupt when the timer
overflows. This allows the software to
perform a function automatically in
the interrupt service routine every
time an overflow occurs. To make this
useful for a real time clock, we have to
do a little calculation.
Crystals come in various frequencies, but clock crystals are mostly
32.768 kHz or 32,768 pulses per
second. If we were to run the Timer1
from a 32.768 kHz crystal, then
Timer1 would overflow to 65536
every two seconds ( 32,768 pulses per
second times 2). For a real time clock,
we would prefer it to overflow every
second and we can make that happen
by presetting Timer1 to $80 hex or
%10000000 binary. This way, on the
32,769th pulse, the Timer1 overflows
and the interrupt occurs.