The Microcontroller Idea Book - Jan Axelson's Lakeview Research
The Microcontroller Idea Book - Jan Axelson's Lakeview Research
The Microcontroller Idea Book - Jan Axelson's Lakeview Research
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 10<br />
an essential timing reference, by itself it doesn’t keep track of real-world time measured in<br />
seconds, minutes, and hours. But if you know the crystal’s frequency, you can measure<br />
seconds by counting the oscillations of the crystal. This is what BASIC-52’s real-time clock<br />
does.<br />
A CLOCK1 statement starts the real-time clock, which causes the TIME operator to<br />
increment every 5 milliseconds. Reading the TIME operator tells you the number of seconds<br />
that have passed since the clock was enabled. CLOCK0 stops the clock and freezes TIME<br />
at its current value. TIME resets to 0 when the count reaches 65536 seconds (18 hours, 12.3<br />
minutes), or when the statement TIME=0 executes. If you stop the clock and then then restart<br />
it, TIME will continue counting from where it left off, unless you first reset it to 0.<br />
<strong>The</strong> ONTIME instruction jumps to a subroutine whenever TIME reaches the value you<br />
specify. Because the ONTIME subroutine is an interrupt routine, you use RETI, not<br />
RETURN, to end it.<br />
Listing 10-1 is a program that counts seconds, minutes, and hours, and displays the current<br />
reading once per second. For accurate timekeeping, the XTAL operator must match the<br />
value of your timing crystal.<br />
You can also use ONTIME to trigger periodic operations. Listing 10-2 is a program that<br />
toggles bit 7 of Port 1 once per second and displays the logic state of the bit after each toggle.<br />
Clock Accuracy<br />
<strong>The</strong> more accurate your timing reference, the more accurate your clock will be. You can tune<br />
the frequency of a crystal slightly by varying the value of one of the capacitors that connects<br />
from the crystal to ground.<br />
Temperature variations will cause a crystal’s frequency to drift. Crystal accuracy is rated in<br />
parts per million per degree Celsius (often shortened to ppm). Over time, a crystal rated at<br />
±10 ppm should vary no more than 0.001 percent per degree Celsius, or 0.86 seconds per<br />
day, if the temperature varies no more than ±1 degree Celsius. If your clock must be<br />
super-accurate, choose the most stable crystal you can find and and avoid temperature<br />
fluctuations.<br />
You might think that you can get a more accurate real-time clock by adjusting XTAL to<br />
match your crystal’s actual frequency, rather than its rated value. You could measure the<br />
crystal’s frequency with a frequency counter, or experiment by varying the value of XTAL<br />
and monitoring the real-time clock to find the best match. For example, if your 12Mhz crystal<br />
actually oscillates at 11.97 Mhz, you could set XTAL equal to 11970000.<br />
172 <strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong>