Clock program based on microcontroller timer interrupt

Publisher:温暖梦想Latest update time:2012-10-17 Source: 21ic Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

As long as you change the port yourself, you can use it, and the time is still relatively accurate.

#include
#define uchar unsigned char
#define uint  unsigned int

sbit RING1=P3^7; //P3^7 connects to buzzer                                       
sbit OPEN=P3^1; //LED lights up when alarm is on                    
uchar idata buffer[8]={0,0,0,0,0,0,10,11}; //define two buffers, buffer is used to temporarily store and initialize the BCD code of the time during the process
                                           

uchar code LED[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X40,0X40}; //LED segment translation is stored in the program storage area
uchar miao=0;fen=58;shi=10; //define global variables for miao,fen,shi for storing process time
uchar _20ms; //define the global variable _20ms to count the number of interrupts, 50 for 1 second
uchar hour2,minutes2,second2; //Alarm setting time storage area
uchar hour1,minutes1,second1; //temporary storage area when modifying time
bit hour,minutes,second; //Modify the corresponding bit flag

bit add,dec; //addition and subtraction bit flags
bit openring, cancelring; //Alarm on, off, cancel flag                                            
bit setring,settime; //Alarm setting, time setting flag                                    
bit ok; // confirm the variable


 void delay_1ms(uchar x) //delay 1ms
{
  flying j;
  while(x--)
    {
      for(j=0;j<123;j++){;}
    }
}
 
 void inital(void) //Timer initialization function
{ RING1=1; //Here, when RING is equal to 0, the buzzer sounds;
   TMOD=0x01; //Timer 0 works in mode 1, using 12M crystal oscillator, timing 20ms
   TH0=(65536-20000)/256;
   TL0=(65536-20000)%256; 
   TR0=1; //TR0=1 starts the timer
   EA=1; //Open the general interrupt
   ET0=1; //Open timer interrupt
}


void timer0 (void) interrupt 1 using 1 //Timer 0 interrupt function
{ TH0=(65536-20000)/256;
   TL0=(65536-20000)%256;
    _20ms++;
    if(50==_20ms) //Judge whether it is one second
       { miao++; // one second has passed, seconds++
         _20ms=0; 

         if(miao==60) //Have you reached sixty seconds?
            { fen++; //It's time++
              miao=0;
          
              if(fen==60) //Has it reached 60 minutes?
                 { shi++; //When the time comes++
                   fen=0;
  
                 if(shi==24) //Has 24 hours arrived?
                     { shi=0; // clear to zero
                       fen=0;
                      miao=0;
                     }
                  }
              }
          }
  if(openring==1) //Is the alarm on?
    { if((shi==hour2)&&(fen==minutes2)&&(miao==second2)) //The alarm goes off when the set time comes
         RING1=0;
    if(cancelring==1) //Alarm off
            {RING1=1;cancelring=0;}
 }  
}

void timebcd (uchar m,uchar f,uchar s) //Time change function, when the timing reaches 1 second, the time will be changed             
{ 
 buffer[0]=m%10; //shi, fen, miao are converted into BCD codes, mainly for the display of digital tubes
 buffer[1]=m/10;
 buffer[2]=f%10;
 buffer[3]=f/10;
 buffer[4]=s%10;
 buffer[5]=s/10;
}


void keyscan(void) //Key scan and determine key value function, this part is the core of various clock functions
{
  uchar scode,recode,value; //Define row and column variables
  P2=0XF0; // Note the connection between keyboard and P2, 4*4 keyboard
  if ((P2 & 0XF0)!=0XF0)
     { 
      delay_1ms(10);
      if ((P2&0XF0)!=0XF0)
         {
           scode=0xfe;
     while((scode & 0x10)!=0)
              {  P2=scode;
                 if ((P2&0XF0)!=0XF0)
                   {recode=(P2 & 0XF0)|0X0f;
                      value=((~scode)+(~recode));
       switch(value)
       { case 0x11: hour=1;minutes=0;second=0;break;//, the key pressed is the function key for modification, and hour is the flag for modification
         case 0x21: hour=0;minutes=1;second=0;break;//Minutes, the key pressed is the function key for modifying minutes, and minutes is the flag for modifying minutes;
         case 0x41: hour=0;minutes=0;second=1;break;//Seconds are similar to above;
         case 0x81: add=1;dec=0;break;// 加   
         case 0x12: dec=1;add=0;break;//减
         case 0x22: setring=1;settime=0;second1=second2;minutes1=minutes2;hour1= hour2;break;//Alarm setting,
         case 0x42: settime=1;setring=0;second1=miao;minutes1=fen;hour1=shi;break;//time setting
         case 0x82: setring=0;settime=0;break;//return
         case 0x14: ok=1;break;//OK
         case 0x24: openring=1;OPEN=0;break;//Open the alarm
         case 0x44: openring=0;OPEN=1;break;//Turn off the alarm
         case 0x84:cancelring=1;break;//Cancel the ringing            
         default: break;
                       }
                   }
   else   scode=(scode<<1)|0x01;
              }
           }     
       } 
} 

  Note: The keyboard values ​​are encoded here. 12 keyboards are encoded. Pay special attention to the implementation methods of alarm setting and time setting. They share the function resettime, which is not difficult.      
void resettime (viod)  
{
 if(hour==1)
   {
     if (add==1)
        {hour1++;add=0;}

     if( hour1==24)
         hour1=0;

     if (dec==1)
         {hour1--;dec=0;}

     if (hour1==-1)
        hour1=23;
   }
  if(minutes==1)
  {
     if (add==1)
      {minutes1++;add=0;}

     if (minutes1==60)
         minutes1=0;

     if(dec==1)
      { minutes1--;dec=0;}

     if (minutes1==-1)
       minutes1=59;
   }

    if(second==1)
  { 
    if (add==1)
      {second1++;add=0;}

    if(second1==60)
      second1=0;

    if (dec==1)
      {second1--;dec=0;}

    if(second1==-1)
      second1=59;
   }

}
 
  

 display (void) //display function subroutine
{  
   flying j,n;
   for(n=0,j=0XFE;n<8;j=((j<<1)|0X01),n++)
     {P1=j; //P1 connects to digital tube position selection
      P0=LED[buffer[n]]; //P0 is connected to the digital tube segment selection
      delay_1ms(5);
     }
}


                          
 void main (void)
{ 
   inital(); // Call timer initialization
  for(;;)                           
  {                        
    timebcd(miao,fen,shi);  

    keyscan(); 
                                       //Call key scan and confirm function
    if(settime==1) //When the key pressed is time setting
      { 
          resettime();
   timebcd ( second1, minutes1,hour1);
 

          if(ok==1) //When the OK button is pressed, it indicates that the reset time will overwrite the original time, so reset miao, fen, shi, and clear _20ms to zero
           { shi=hour1;
      fen=minutes1;
      miao=second1;
             _20ms=0;
                                                  }
      } 

   if(setring==1)
   {   
      resettime();
      timebcd( second1, minutes1,hour1);

         if(ok==1)
           {  hour2=hour1;
       minutes2=minutes1;
       second2=second1;
                                                         }
      }

     display(); //Call display function

    }
}

Keywords:MCU Reference address:Clock program based on microcontroller timer interrupt

Previous article:MCU decoding 315M pt2262 encoded c51 program
Next article:Design of electronic clock based on stc51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 16:02

Design of heat meter based on single chip microcomputer and serial bus technology
  The metering and charging of heating for residents in my country has become a general trend, but it is also a hot and difficult issue. The accuracy of the measurement is directly related to the reasonable charging of heat and the vital interests of users, and also to the survival and development of the heating indus
[Microcontroller]
Design of heat meter based on single chip microcomputer and serial bus technology
The two major series of microcontrollers are PK_MSP430 and AVR
  There are many types of microcontrollers, and many manufacturers have launched their own MCUs. Among the many brands of microcontrollers, I prefer the MSP430 and AVR series. The following is a PK analysis based on their respective characteristics and several aspects.   Since we are doing a PK, just like a sports c
[Microcontroller]
The two major series of microcontrollers are PK_MSP430 and AVR
MCU Learning VII: Basic I/O Port Experiment 3 - Left and Right Marquee
1. Experimental phenomenon: The diode changes from left to right, and then from right to left like a marquee, and the interval between light changes is 1s.   2. Experimental Objectives: l Master the application of left and right circular shift instructions rlc and rrc with carry l Further familiarize yourself wi
[Microcontroller]
MCU Learning VII: Basic I/O Port Experiment 3 - Left and Right Marquee
PIC series microcontroller application design and examples
1. Introduction As the application of microcontrollers becomes increasingly widespread, applications in various fields have also put forward higher requirements for microcontroller manufacturers, hoping for faster speed, lower power consumption, smaller size, lower price, and fewer peripheral devices required when co
[Industrial Control]
Interface Design between P51XA Single Chip Microcomputer and Graphic LCD Display
Interface Design between P51XA Single Chip Microcomputer and Graphic LCD Display P51XA is a 16-bit single-chip microcomputer of PHILIPS Company, with large manageable memory space, fast running speed, support for real-time multi-tasking system, enhanced support for high-level language, and can be used in occasions r
[Microcontroller]
Interface Design between P51XA Single Chip Microcomputer and Graphic LCD Display
The first MCU stock in the signal chain! Chipsea Technology's IPO on the Science and Technology Innovation Board passed the review
On July 17, the Shanghai Stock Exchange Science and Technology Innovation Board Stock Listing Committee held the 54th review meeting of 2020. According to the review results, Xinhai Technology's Science and Technology Innovation Board IPO was successfully passed. According to the prospectus, Xinhai Technology was fo
[Mobile phone portable]
The first MCU stock in the signal chain! Chipsea Technology's IPO on the Science and Technology Innovation Board passed the review
Homemade MCU STC development board
 STC Bearings GMBH is one of the important member companies in the development of transmission and control technology in Europe. Founded in 1927, it has been committed to the production and manufacturing of precision bearings, heavy-duty bearings and seats, bushings, seals, etc. Its customers are spread all over the wo
[Microcontroller]
Homemade MCU STC development board
Construction of embedded remote power grid monitoring system based on SX52BD microcontroller
This paper introduces the specific scheme of using UBICOM's SX52BD single- chip microcomputer to build an embedded system for remote power grid monitoring, so that the measurement and control equipment based on the single-chip microcomputer can be easily connected to the Ethernet to implement remote netw
[Microcontroller]
Construction of embedded remote power grid monitoring system based on SX52BD microcontroller
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号