51 MCU 4-bit digital tube electronic clock source program

Publisher:BlissfulMoonLatest update time:2015-06-16 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/****************************************************** ***************************
Title: Timer interrupt 24-hour clock program

Effect: Digital tube displays 24-hour clock (with time adjustment function)

Note: Use 12M crystal oscillator, key1=P3^7; key2=P3^6; key3=P3^5; key4=P3^4;

                  Digital tube position selection P1.0~P1.3, segment selection P0

*************************************************** ****************************/

 

#include

 

#define uchar unsigned char;

#define uint unsigned int;

                                              

 

uchar position;           

uchar tt;             

uchar second;       

uchar minute;

uchar hour;       

 

 

uchar code table[]=

                            {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9

 

                  

sbit smg_q = P1^0; //digital tube thousands digit pin

sbit smg_b = P1^1;

sbit smg_s = P1^2;

sbit smg_g = P1^3;

 

sbit key1=P3^7; //button definition

sbit key2=P3^6;

sbit key3=P3^5;

sbit key4=P3^4;

 

 

void keyscan(); //Function declaration

void display(unsigned char hour, unsigned char minute);

void delay(unsigned int timer);

void init();                                              


/****************************************************** **********

                                     Main function

*************************************************** *********/

void main()

{

         init();                                    

         while(1)

         {

                   if(tt==20)

                   {                          

                            tt=0;

                            second++;

                            if(second==60)

                            {

                                     second=0;

                                     minute++;

                                     if(minute==60)

                                     {       

                                               minute=0;

                                               hour++;

                                               if(hour==24)

                                                        hour=0;

                                     }

                            }

                   }

 

                   keyscan();

                   display(hour,minute);      

                   delay(1);

         }

}

/****************************************************** **********

                                     Time Adjustment

*************************************************** *********/

void keyscan()

{

         if(key1==0) //hour plus 1

         {

                   TR0=0;

                   hour++;

                            if(hour==24)

                                     hour=0;

                   delay(200) ;

                   TR0=1;

         }

         if(key2==0) // hours minus

         {

                   TR0=0;

                   hour--;

                            if(hour<0)

                                     hour=23;

                   delay(200) ;

                   TR0=1;

         }

 

         if(key3==0) // add minutes

         {

                   TR0=0;

                   minute++;

                            if(minute==60)

                                     minute=0;

                   delay(200) ;

                   TR0=1;

         }

 

                   if(key4==0) // minutes minus

         {

                   TR0=0;

                   minute--;

                            if(minute<0)

                                     minute=59;

                   delay(200) ;

                   TR0=1;

         }

}

 

/****************************************************** **********

                                                initialization

*************************************************** *********/

void init()

{

         tt=0;

         position=0;               

         second=0;

         minute=0;        

         hour=0;   

         smg_q=1;

         smg_b=1;

         smg_s=1;

         smg_g=1;

         key1=1;

         key2=1;

 

         TMOD=0X01;  

         TH0=(65536-50000)/256;        

         TL0=(65536-50000)%256;

         EA=1;                

         ET0=1;              

         TR0=1;              

}

 

/****************************************************** **********

                                                  Timer TO interrupt

*************************************************** *********/

void t0() interrupt 1

{

         TH0=(65536-50000)/256;        

         TL0=(65536-50000)%256;

         tt++;                                              

}

 

/****************************************************** **********

                                          show

*************************************************** *********/

void display(unsigned char hour, unsigned char minute)

{

         P0=0XFF;                             

         switch(position)

         {

         case0:              

                   smg_g=1;

                   smg_q=0;

                   P0=table[hour/10];

                   break;

 

         case1:

                   smg_q=1;        

                   smg_b=0;        

                   P0=table[hour%10];

 

                   if(tt>=10)

                            P0&=0x7f; //The decimal point flashes for half a second

                   break;

 

         case2:    

                   smg_b=1;                  

                   smg_s=0;                  

                   P0=table[minute/10];

                   break;

         case3:                       

                   smg_s=1;

                   smg_g=0;

                   P0=table[minute%10];

                   break;

         }

         position++;               

         if(position>3)

                   position=0;      

}

 

/****************************************************** **********

                                                 Delay

*************************************************** *********/

void delay(unsigned int time)

{                

         unsignedint x,y;

         for(x=time;x>0;x--)

                   for(y=110;y>0;y--);

}
Reference address:51 MCU 4-bit digital tube electronic clock source program

Previous article:51 MCU timer timing mode
Next article:Single chip electronic keyboard assembly program

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号