Electronic clock that can adjust time-C language

Publisher:温柔的爱情Latest update time:2012-09-14 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Using stc89c52 microcontroller, the circuit is very simple, using eight-bit common anode digital tube, P0 port is the segment selection of the digital tube, P2 port is the bit selection. Use key1-key3 to adjust the time

Key points:
1. Modularization of functions
2. Interruption

#include
#define uchar unsigned char
#define uint unsigned int
sbit p2_0=P2^0;
sbit p2_1=P2^1;
sbit p2_2=P2^2;
sbit p2_3=P2^3;
sbit p2_4=P2^4;
sbit p2_5=P2^5;
sbit p2_6=P2^6;
sbit p2_7=P2^7;
sbit key0=P3^2;
sbit key1=P3^3;
sbit key2=P3^4;
sbit key3=P3^5;
sbit p17=P1^7;
uint t=0;  
uint key=0;
uchar set=0;
uchar second=00,minter=55,hour=11;
uchar second_1,second_2,minter_1,minter_2,hour_1,hour_2;
const uchar tab[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf} ; //0~~9 digital table

void delay() //Delay subroutine
  { uint i;
   
      for(i=0;i<240;i++);

  }
 void time_0(void) interrupt 1 using 0 //Internal timer 0 internal timing 50ms
  {
     TH0=0x3c;
  TL0=0xb0;
  t++; // t is the number of times the timer is counted
  }
   
 
  void display(uchar secondd,uchar minterr,uchar hourr) //Subroutine to display the time
  {
    second_1=secondd/10;
 second_2=secondd%10;
 minter_1=minterr/10;
 minter_2=minterr%10;
 hour_1=hourr/10;
 hour_2=hourr%10;
 P0=tab[second_2];
 p2_0=0;
 delay();
 p2_0=1;     
 P0=tab[second_1];
 p2_1=0;
 delay();
 p2_1=1;

 p2_2=0;
 P0=0xbf;
 delay();
 p2_2=1;

 P0=tab[minter_2];
 p2_3=0;
 delay();
 p2_3=1;     
 P0=tab[minter_1];
 p2_4=0;
 delay();
 p2_4=1;

 p2_5=0;
 P0=0xbf;
 delay(); //The delay is to make — “——” appear, otherwise the time is too fast, —
 p2_5=1;

 P0=tab[hour_2];
 p2_6=0;
 delay();
 p2_6=1;     
 P0=tab[hour_1];
 p2_7=0;
 delay();
 p2_7=1;
  }
  void time()
  {
   
          //Has the 1s timing reached?
      if(t==19) //1s is up, add one second
     { t=0;
       second++;
       if(second==60) // Is it 60 seconds? If so, reset the seconds to zero and add one second to the minutes. If not, exit.
    {
              second=0;
      minter++;
      if(minter==60) // Is the minute 60? If yes, clear the minute and add one to the hour; if no, exit
      { minter=0;
          hour++;
       if(hour==24)
        {
   
        hour=0;
        }
      }
     }
     }
   
   display(second,minter,hour); //Call the display function to display the time
  }
    
uint scan_key() //Adjust time button scanning function
    { //Delay
   delay(); //Judge if key3 is pressed, return 1 to call the time adjustment function, if it is not zero, return 0 and do not adjust
   if(key0==0)
     set=1;
    if(key0==1)
      set=0;
    return set;
 }
  
   void set_time() // adjust time function
     { uchar m;
    if(key1==0) delay(); //Adjust the hour, confirm key1 is pressed
    if(key1==0) hour++; // x hours increment
    if(hour==24) hour=0; //Hours count to 24, starting from zero
    for(m=0;m<20;m++) //Loop 20 times
     { time(); //Call the display function to display the time
            
  }
    if(key2==0) delay(); //Adjust minutes
    if(key2==0) minter++;
    if(minter==60) minter=0;
     delay();
       time(); //Call the time function to display the time
     if (key0 == 0)

     time();        
            
  }
  void main() //The main function sets the internal timer
  {
     TMOD=0X01;
      TH0=0X10;
      TL0=0X00;
      EA=1;
      ET0=1;
      TR0=1;
   P2=0xff;
    
      while(1)
    {
      if(key0==0) // p determines whether the time adjustment button is pressed, that is, whether the time needs to be adjusted
    {
       p17=0;
    delay();
    delay();
    
    delay();
    p17=1;
    delay();
     delay();
      key=scan_key();
  
     }
   
   switch(key)
   { //Has the 1s timer expired?
         case 0: time(); break;
   case 1: set_time();break;
   default :break;
   }
    }
      
  }
Reference address:Electronic clock that can adjust time-C language

Previous article:Digital tube dynamic display program
Next article:Digital tube water light display data

Recommended ReadingLatest update time:2024-11-16 15:21

Use of timer of STC89C52 microcontroller
///////////////////////////////////////////////////// ////////////////////////   Implementation function:  Configure the relevant registers of timer 2 to realize the timer interrupt function, and then                         implement an accurate clock system according to the interrupt cycle frequency, and display th
[Microcontroller]
STC89C52 MCU stepper motor debugging
///////////////////////////////////////////////////// //////////////////////////// Function: First, let the stepper motor on the experimental board accelerate from the positive direction - uniform speed - deceleration, then         stop the stepper motor, then let the stepper motor accelerate from the negative direct
[Microcontroller]
STC89C52 MCU Timer 2
Timer 2: 16-bit timer counter with auto-reload and capture capability Control register TCON2: Byte address------------- 0C8H--------------------- Bit addressable TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2 Overflow flag T
[Microcontroller]
[51 microcontroller] STC89C52 buzzer experiment, including c code (4)
1. Reference tutorial: Qingxiang 51 microcontroller tutorial 2. Basic principles: If vcc is 5v and p23 is low, causing a current greater than 1mA to flow through R10, CE will be turned on and the buzzer will sound. P23 is high frequency and vice versa. 3.C program code: #include reg52.h #include intrins.h //Stan
[Microcontroller]
[51 microcontroller] STC89C52 buzzer experiment, including c code (4)
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号