Wednesday, 24 July 2019
Main Menu
Home Home
Shop Shop
News News
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login


If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News

AN #32 - Realtime clock with LED display Print
Realtime clock with LED display and 89S8252

A real time clock using six 7-segment displays is used in this project to display the time in HH-MM-SS format using a micro controller ATMEL-89s8252 with minimum number of external components. Five switches are used to set the HOURS MINUTES (+/-) and one for zero reset of the clock display. The micro controller runs of a 6Mhz crystal which also acts as the time base for the clock.

ATMEL 89s8252 is a derivative of the famous 8051 micro offering more features and is downward compatible and runs almost all the software written for the 8051.

The software is written in BASCOM-51 , a powerful BASIC Compiler language designed  by MSC ELECTRONICS, Netherlands. The compiler can be downloaded from their web site as a demo version which is capable of creating a hex file ready to be programmed into the chip using any commonly available programmer or kit.

Most of the 8051 kits today in the market is shipped with 89s8252 and they can be used to program the chip with the software source code listed in this project.

Dedicated clock chips like the NS5314 are hard to obtain now and the micro controllers have taken over with their easy availability, low cost and shareware  programming tools.


INTRODUCTION – A brief over view of the chip 89s8252 ( from the data sheet)


See for more info


  • Compatible with MCS-51™ Products
  • 8K Bytes of In-System Re-programmable Downloadable Flash Memory
  • SPI Serial Interface for Program Downloading
  • Endurance: 1,000 Write/Erase Cycles
  • 2K Bytes EEPROM – Endurance: 100,000 Write/Erase Cycles
  • 4V to 6V Operating Range
  • Fully Static Operation: 0 Hz to 24 MHz
  • Three-level Program Memory Lock
  • 256 x 8-bit Internal RAM
  • 32 Programmable I/O Lines
  • Three 16-bit Timer/Counters
  • Nine Interrupt Sources
  • Programmable UART Serial Channel
  • SPI Serial Interface
  • Low-power Idle and Power-down Modes
  • Interrupt Recovery From Power-down
  • Programmable Watchdog Timer
  • Dual Data Pointer
  • Power-off Flag




The AT89S8252 is a low-power, high-performance CMOS 8-bit microcomputer with 8K bytes of downloadable Flash programmable and erasable read only memory and 2K bytes of EEPROM. The device is manufactured using Atmel’s high-density nonvolatile memory technology and is compatible with the industry-standard 80C51 instruction set and pinout. The on-chip downloadable Flash allows the program memory to be reprogrammed in-system through an SPI serial interface or by a conventional nonvolatile memory programmer. By combining a versatile 8-bit CPU with downloadable Flash on a monolithic chip, the Atmel AT89S8252 is a powerful microcomputer which provides a highly-flexible and cost-effective solution to many embedded control applications.

 The AT89S8252 provides the following standard features: 8K bytes of downloadable Flash, 2K bytes of EEPROM, 256 bytes of RAM, 32 I/O lines, programmable watchdog timer, two data pointers, three 16-bit timer/counters, a six-vector two-level interrupt architecture, a full duplex serial port, on-chip oscillator, and clock circuitry. In addition, the AT89S8252 is designed with static logic for operation down to zero frequency and supports two software selectable power saving modes. 

Circuit Explanation 


The Power supply uses a 7805 to step down 12volts to a stable 5 VDC to power the circuit.

AT89S8252  has 4 ports of which we use 3.

Port 0  is used to drive the segments of all the 7-segment common cathode displays connected as a bus. 74ls244 acts as an octal buffer between the micro and the displays to increase the current capacity. 330 ohm resistors are used to limit the segment led current.

Port 2 is used to act as a multiplexer to select a particular display using ic 2803 ( 8 transistor array)

10 k pull up resistors are used on both these ports as recommended in the data sheet.

On a pcb we can use resistor packs that are more convenient to solder or separate resistors can be used.

Capacitor C5 and R2 act as a power on reset combination and a separate reset switch is also available.

Port 1 is used for inputs – to detect key presses to increment or decrease hours/minutes and to reset the display to 00:00:00.

Simple push button switches are used to PULL DOWN the port pins to ground and it is upto the software to detect this activity and set the time accordingly. All micro controllers have this strange arrangement of a logic LOW to indicate an active external input

Pull up resistors on port 1 has been avoided since the port has internal pull ups.

Port 3 is not used in this project.

With this minimum component count, it is upto the software to do the multiplexing , seven segment conversion and display the time on the 7 segment displays.


Software  Explanation

Written in BASCOM-51 version , the software is embedded with comments wherever necessary .

Readers with knowledge in IBM / Microsoft’s BASIC / BASIC-A / GW-BASIC / QBASIC that used to run on the good old 286 and 386 PCs with DOS 2.x to 6.2  will understand the program very easily. More details on the language syntax is available from MSC ELECTRONICS web site.



  1. Description of port pins and where they are connected
  2. Inclusion of the header file for the micro
  3. Definition of crystal speed
  4. Declaration of variables as BITS, BYTES and WORDS
  5. Initialise all ports to 0 except port-1 which is turned HIGH to act as an input port.
  6. Run a diagnostic subroutine to test all the digit’s segments.
  7. Configuration of internal timer to act as an interrupt generator to get a one second activity source.
  8. Initialise hours / minutes / seconds variables to zero.
  9. Get into a perpetual DO LOOP to display the time in HH:MM:SS format
    (Note : Since there are no bcd to 7-segment converter Ics and no latch Ics, it is upto the software now to  show the clock display without being interrupted .
  10. Set the input switches to activate the respective sub routines using the built-in command of BASCOM’s key de-bounce statement
  11. Check for Seconds/ Minutes/ Hours variables on exceeding their limits and increment them accordingly.
  12. Activate the digits one-by-one through port-2 and show the corresponding number on the display using port-0.
  13. Declaration of key press subroutines
    (check for the key pressed to adjust the variables for hours/minutes)
  • The main display subroutine
    concept  is performed to get the correct byte value for the digit to be displayed.Note: since we do not have a seven segment converter IC, a quick table check using the READ/DATA
  • The internal timer interrupt subroutine - This subroutine is called 2000 times a second with a 6 Mhz crystal and to generate an accurate once a second variable, we set the flag only once every 2000 times.

This variables is used to detect the second change and increments the time in the main DO-LOOP routine.

The accuracy of the clock depends on the timer subroutine . A detailed study of BASCOM and the TIMER0/1 functions of the Micro will help a great deal.

Future Scope

With the power of the Micro and the Language it is easy for readers to improve the software and circuit and convert it  into an alarm clock .

With port-3 activated , it can be used as a multi channel industrial timer.

Download complete article with PDFs and Documents here

Rest is left to the reader.
K.S.Sankar, Mostek