Software-Based Real Time Clock (RTC)
What is a Real Time Clock? (RTC)
A Real-Time-Clock (RTC) is, as the name suggests, a clock which keeps track of time in a "real mode."
While there are a number of 8051-compatible microcontrollers that have built-in, accurate real-time clocks
(especially from Dallas Semiconductor), some simple applications may benefit from a software RTC
solution that uses the built-in capabilitites of an 8051 microcontroller.
This page will go through the development of a simple software-base RTC solution using 8051 Timer 1
(T1). Thus, your software application will have the benefit of an RTC without requiring any additional
hardware.
What are the drawbacks of a software-based RTC?
The drawback to this or any other similar software-based RTC is accuracy: This software RTC is based on
the 8051 Timer. The 8051 Timer, in turn, is based on the crystal speed used in your application. Thus there
are two potential (and real) issues that you need to take into consideration:
� Your software will require a known crystal speed. If you change the crystal speed connected to
your 8051, you will have to modify the software accordingly.
� The accuracy of our RTC will only be as accurate as the crystal you use.
The variation of an RTC compared to the current time is called "drift" and is often measured in "seconds of
drift per month." A specification may indicate that a given hardware RTC is accurate "+/- 10 seconds per
month." If you are going to use a software-based RTC, such as this one, be sure your crystal is rated with
minimal variation.
Step 1: Our Variables
Before we start developing the code, lets get a few variables established. These variables will be used
frequently within interrupts, so it is a good idea to put them in Internal RAM. To make this code as non-
instrusive as possible, we'll locate our variables at the end of Internal RAM (07Ch-07Fh).
HOURS EQU 07Ch
MINUTES EQU 07Dh
SECONDS EQU 07Eh
TICKS EQU 07Fh
Our interrupt will use these four variables to keep track of time. Additionally, your main program may
access these variables whenver it wishes to determine the "current time" from the RTC.
Step 2: The Crystal Frequency
The next thing we need to take into account is the speed of the crystal being used. Keep in mind that with a
crystal of 11.0592Mhz, Timer 1 will increment 11,059,200/12=921,600 times per second.
NOTE: The standard 8051 Timer increments every 12 crystal cycles. However, some derivative
chips increment their timers after a different number of crystal cycles: For example, Dallas
microcontrollers can be programmed to increment every 4 cycles. If you are using a derivative that
uses some value other than 12, you will have to make the appropriate changes to this code.
Let's establish some more equates to make our code more portable: