Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Digital Clock with time set option using seven segment and 8051 microcontroller

By Himanshu Choudhary

This digital clock not only displays time (on four seven segment displays) but also provides the user an option to set the time. For this the user has to first press the reset switch after which he/she can select and set a particular digit by incrementing its value. To run the clock with the set time, the user needs to press the start button. The seven segment and switches are interfaced with microcontroller AT89C51.


This idea to provide option for setting time can be implemented in conjunction with the digital clock circuit.  The control options for time setting are provided by means of tactile switches which are made active low. This circuit uses four such switches for following functions.

 

 

Switch 1 (S1)   :           Reset               (to initiate the time set option)
Switch 2 (S2)   :           Select              (to select the segment where value is to be changed)
Switch 3 (S3)   :           Increment        (to increase the value at selected segment)
Switch 4 (S4)   :           Start                (to start the clock with the set time options)
 
As soon as S1 is pressed, the running clock stops and it goes into the reset mode where the first segment is activated. The segment to be set can be selected in cyclic order each time S2 is pressed. After selecting the desired segment, its value can be changed by using S3. Once the digits and hence the time is set, S4 is pressed to start the clock again.
The seven segments are interfaced to port P2 of the microcontroller through its data pins (a – h). The enable pins are connected to pins 1-4 of port P1 (P1^0 – P1^3). The switches S1-S4 are connected as following to provide ground to the corresponding pins of port P1:
S1         :           P1^4
S2         :           P1^5
S3         :           P1^6
S4         :           P1^7
 
 

Project Source Code

###

// Program to make a clock with time setting functionality.

#include<reg51.h>
 sbit dig_ctrl_4=P1^0;        //Declare the control pins of the seven segment
 sbit dig_ctrl_3=P1^1;
 sbit dig_ctrl_2=P1^2;
 sbit dig_ctrl_1=P1^3;
 sbit reset=P1^4;        // Reset pin to reset the clock.
 sbit start=P1^7;        //Start pin to start the clock after the time is set.
 sbit incr=P1^6;        //Increment pin to increase the digits during time setting.
 sbit set=P1^5;        // Set pin to set the time.
 int sel_seg_to_incr=0;
 int ret_seg_to_incre=0;
 int recnt_incr_seg;
 unsigned char dig_disp=0;
 int min2=0;
 int min1=0;
 int sec2=0;
 int sec1=0;
 char dig_val[10]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x10};        // to store values corresponding to digits 0 to 9

void delay(int a)        // Function to provide a time delay of approx. 1 second using Timer1.
{
    int i;
    for(i=0;i<a;i++)
    {
        TL1=0xFD;
         TH1=0x4B;
         TR1=1;
         while(TF1==0);
         TR1=0;
         TF1=0;
    }                                   
 }

int setfn()        // Function to select minute and seconds digit to be set.
{                     
    while(set==0)
    {
        switch(recnt_incr_seg)
        {
            case 1:
            if(set==0)        //select the min2 digit
            {
                dig_ctrl_4=1;
                dig_ctrl_3=0;
                dig_ctrl_2=0;
                dig_ctrl_1=0;
                recnt_incr_seg=1;
                ret_seg_to_incre=1;
                P2=dig_val[min2];
                delay(20);
            }

            case 2:
            if(set==0)        // Select the min1 digit
            {
                dig_ctrl_4=0;
                dig_ctrl_3=1;
                dig_ctrl_2=0;
                dig_ctrl_1=0;
                recnt_incr_seg=2;
                ret_seg_to_incre=2;
                P2=dig_val[min1];
                delay(20);
            }
       
            case 3:
            if(set==0)        //Select the sec 2 digit
            {
                dig_ctrl_4=0;
                dig_ctrl_3=0;
                dig_ctrl_2=1;
                dig_ctrl_1=0;
                recnt_incr_seg=3;
                ret_seg_to_incre=3;
                P2=dig_val[sec2];
                delay(20);
            }
       
            case 4:
            if(set==0)        //Select the sec1 digit
            {
                recnt_incr_seg=1;
                dig_ctrl_4=0;
                dig_ctrl_3=0;
                dig_ctrl_2=0;
                dig_ctrl_1=1;
                ret_seg_to_incre=4;
                P2=dig_val[sec1];
                delay(20);
                recnt_incr_seg=1;
            }
        }
    }
    return(ret_seg_to_incre);
}

void increase(int a)        //Function to set the minute or seconds digit
{
    while(incr==0)
    {
        switch(a)
        {
                case 1:        // To set the min2 digit.
                P2=dig_val[min2];        //Display the value of min2
                delay(15);
                min2++;
                if(min2==6)
                min2=0;
                P2=dig_val[min2];
                delay(5);
                break;
               
                case 2:        //To set the min1 digit.
                P2=dig_val[min1];
                delay(15);
                min1++;
                if(min1==10)
                min1=0;
                P2=dig_val[min1];
                delay(5);
                break;
       
                case 3:        // To set the sec2 digit.
                P2=dig_val[sec2];
                delay(15);
                sec2++;
                if(sec2==6)
                sec2=0;
                P2=dig_val[sec2];
                delay(5);
                break;
       
                case 4:        //To set the sec1 digit.
                P2=dig_val[sec1];
                delay(15);
                sec1++;
                if(sec1==10)
                sec1=0;
                P2=dig_val[sec1];
                delay(5);
                break;
        }
    }
}

void resetfn()        // This function brings the clock to reset or set mode.
{
    IE=0x80;        //Disable timer0 interrupt to stop the display of the clock
    sel_seg_to_incr=1;
    recnt_incr_seg=1;
    dig_ctrl_4=1;
    dig_ctrl_3=0;
    dig_ctrl_2=0;
    dig_ctrl_1=0;
    P2=dig_val[min2];
    delay(5);
    while(1)
    {             
        if(start==0)        //check if start pin is pressed
        {
             TMOD=0x11;        //reset the timer0
            TL0=0xf6;
            TH0=0xFf;
            IE=0x82;        //enabling again the timer0 interrupt to start displaying of the clock
            TR0=1;
            break;        // break loop and return to main program
        }
       
        if(set==0)        //if set pin is pressed call set function
        sel_seg_to_incr=setfn();
       
        if(incr==0)        // if incr pin is pressed call incr function
        increase(sel_seg_to_incr);
    }
}
void display() interrupt 1        // function to display the values on seven segmnet. For more details refer seven segment multiplexing.
{
    TL0=0x36;        //reloading timer0
    TH0=0xf6;
       P2=0xFF;
    dig_ctrl_1 = dig_ctrl_3 = dig_ctrl_2 = dig_ctrl_4 = 0;
    dig_disp++;
    dig_disp=dig_disp%4;
    switch(dig_disp)
    {
        case 0:
        P2=dig_val[sec1];
        dig_ctrl_1 = 1;
        break;
   
        case 1:
        P2=    dig_val[sec2];
        dig_ctrl_2 = 1;
        break;
   
        case 2:
        P2=    dig_val[min1];
        dig_ctrl_3 = 1;
        break;
   
        case 3:
        P2=    dig_val[min2];
        dig_ctrl_4 = 1;
        break;
    }
}

void main()        //main function the programs begins from here     
{
    set=1;        //delaring set, reset, start and incr as input pins
    reset=1;
    start=1;
    incr=1;
    TMOD=0x11;        //enabling timer0
    TL0=0xf6;        //loading timer0
    TH0=0xFf;
    IE=0x82;        //initialize timer0 interrupt
    TR0=1;        //triggering timer0
     while(1)        //forward counting of clock
     {
          while(min2<6)
          {                                     
               while(min1<10)
                {   
                 while(sec2<6)
                {
                    while(sec1<10)
                    {
                     if(reset==0)
                     resetfn();
                    delay(20);
                    sec1++;
                    }
                sec1=0;
                sec2++;
                }
            sec1=0;
            sec2=0;
            min1++;
            }
        sec1=0;
        sec2=0;
        min1=0;
        min2++;
        }
    min2=0;
    }
}

###


Circuit Diagrams

Circuit-Diagram-of-Seven-segment-based-digital-clock-with-time-set-option-using-8051-microcontroller-AT89C51

Project Components

  • AT89C51 Microcontroller
  • Seven Segment Display
  • Transistor BC547

Project Video


Filed Under: 8051 Microcontroller
Tagged With: 8051, clock, digital clock, display, microcontroller, seven segment
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • MicroPython – I2C protocol in ESP8266 and ESP3
  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Manifest in Git bitbucket
  • What's the deal with all these "MPPT" IC's with no current sense?
  • Photovoltaic MOSFET Drivers - Voltage Rating
  • Impedance requirement for SDRAM signals
  • A circuit that can adjust a resistance and probing a voltage node

RSS Electro-Tech-Online.com Discussions

  • How to quickly estimate lead acid battery capacity ?
  • IRS2453 the H circuit
  • Ampro 16mm Stylist projector woes.
  • Finally switched to Linux.
  • Multistage BJT amplifier
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering