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:
CRYSTAL EQU 11059200 ;The crystal speed
TMRCYCLE EQU 12 ;The number of crystal cycles per timer increment
TMR_SEC EQU CRYSTAL/TMRCYCLE ;The # of timer increments per second
Thus, should our crystal frequency change or should we move our code to a derivative microcontroller that uses some other value than 12, we simply need to modify our constants.
Step 3:Calculating the Timer 1 Overflow Frequency
Remember, a 16-bit timer will count from 0 to 65,535 before resetting. This is important when you consider that Timer 1 will be incremented 921,600 times per second. Obviously it will overflow it’s 65,535 maximum value a number of times in the course of one second-to be exact, it will overflow 921600/65536=14 times per second. If we were to use the timer in 8-bit or auto-reload mode, the timer would end up overflowing 3599 times per second, which is a lot harder to keep track of.
To display time clock
To control timers
For attendance system- in attendance system we can use this project to show student’s attendance at particular time.