8-bit digital tube display electronic clock c51 program

Publisher:自在逍遥Latest update time:2012-09-08 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*
The 8-digit digital tube displays the time in the format of 05-50-00, indicating 05:50:00
S1 is used for hour plus 1 operation
S2 is used for hour minus 1 operation
S3 is used for minute plus 1 operation
S4 is used for minute minus 1 operation
*/

#include
sbit KEY1=P3^0; //Define port parameters
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
sbit LED=P1^2; //Define indicator light parameters


code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //Common cathode digital tube 0-9
unsigned char StrTab[8]; //define buffer
unsigned char minute=19,hour=23,second; //define and initialize to 12:30:00


void delay(unsigned int cnt)
{
        while(--cnt);
}

/******************************************************************/
/* Display processing function */
/******************************************************************/
void Displaypro(void)
{

        StrTab[0]=tab[hour/10]; //Display hour
        StrTab[1]=tab[hour%10];
        StrTab[2]=0x40; //display "-"
        StrTab[3]=tab[minute/10]; //Display minutes

        StrTab[4]=tab[minute%10];
        StrTab[5]=0x40; //display "-"
        StrTab[6]=tab[second/10]; //Display seconds

        StrTab[7]=tab[second%10];
}

main()
{
        TMOD |= 0x01; //Timer 0 10ms inM crystal for timing
        TH0=0xd8; //initial value
        TL0=0xf0;
        ET0=1;
        TR0=1;

        TMOD |= 0x10; //Timer 1 is used for dynamic scanning
        TH1=0xF8; //initial value
        TL1=0xf0;
        ET1=1;
        TR1=1;
        EA = 1;
        Displaypro(); //Call display processing function

        while(1)
        {
                if(!KEY1) //key 1 debounce and action
                {
                        delay(10000);
                        if(!KEY1)
                        {          
                                hour++;

                                if(hour==24)

                                        hour=0; //normal time hour plus 1
                                Displaypro();
                        }
                }

                if(!KEY2) //Key 2 debounce and action
                {
                        delay(10000);
                        if(!KEY2)
                        {
                                hour--;

                                if(hour==255)

                                        hour=23; //Normal time hour minus 1
                                Displaypro();
                        }
                }
                if(!KEY3) //key debounce and action
                {
                        delay(10000);
                        if(!KEY3)
                        {
                                minute++;

                                if(minute==60)

                                        minute=0; //minute plus 1
                                Displaypro();
                        }
                }       
                if(!KEY4) //key debounce and action
                {
                        delay(10000);
                        if(!KEY4)
                        {
                                minute--;

                                if(minute==255)

                                        minute=59; //minus 1
                                Displaypro();
                        }
                }
        }
}


/******************************************************************/
/* Timer 1 interrupt */
/******************************************************************/
void time1_isr(void) interrupt 3 using 0 //Timer 1 is used for dynamic scanning
{
        static unsigned char num;
        TH1=0xF8; //Re-enter initial value
        TL1=0xf0;

        switch (num)

        {
                case 0:P2=0;P0=StrTab[num];break; //Call the buffer values ​​for scanning
                case 1:P2=1;P0=StrTab[num];break;
                case 2:P2=2;P0=StrTab[num];break;
                case 3:P2=3;P0=StrTab[num];break;
                case 4:P2=4;P0=StrTab[num];break;
                case 5:P2=5;P0=StrTab[num];break;
                case 6:P2=6;P0=StrTab[num];break;
                case 7:P2=7;P0=StrTab[num];break;
                default:break;
        }
        num++; //Scan 8 times, using 8 digital tubes
        if(num==8)
                num=0;
}


/******************************************************************/
/* Timer 0 interrupt */
/******************************************************************/
void tim(void) interrupt 1 using 1
{
        static unsigned char count; //define internal local variables

        TH0=0xd8; //Reassign value
        TL0=0xf0;
        count++;
        switch (count)
        {
                case 0:
                case 20:
                case 40:
                case 60:
                case 80:Displaypro();break; //Call display processing at regular intervals
                case 50:P1=~P1;break; //LED flashes for half a second
                default:break;
        }

        if (count==100)
        {
                count=0;
                second++; //second plus 1
                if(second==60)
                {
                        second=0;
                        minute++; // add 1 to the minute
                        if(minute==60)
                        {
                                minute=0;
                                hour++; // add 1 to the hour
                                if(hour==24)
                                        hour=0;
                        }  
                }
        } 
}
Reference address:8-bit digital tube display electronic clock c51 program

Previous article:Read the matrix keyboard and use the digital tube to display the corresponding key value
Next article:Advertising running light assembler

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号