1602LCD digital electronic clock C language

Publisher:legend9Latest update time:2016-09-12 Source: eefocusKeywords:1602LCD Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1602LCD digital electronic clock basic design ideas:
 
1. 1602 LCD display: 
   First line: Year, Month, Day 2012-01-22
   Second line: hours, minutes, seconds 23:59:00 
 
2. Independent button functions: 
   Key1 restores the actual time before the MCU adjusts the time. Since timer T0 is paused during the time adjustment, timer T1 is started to ensure that the time of the MCU is correct. (That is, cancel the time adjustment)
   Key2 enters the time adjustment state: stop timer T0 and start timer T1.
   Key3 exits the time adjustment state: starts timer T0 and stops timer T1.
   Key4 Select to adjust the time: Seconds, Minutes, Hours, Days, Months, Years
   Key5 Timing adjustment: Incremental and start the buzzer
   Key6 Time adjustment: Decrease and start buzzer
 
It took me two and a half days to finally realize the clock according to my design ideas. During these two days, I felt that the most difficult thing was that when an error occurred, I didn't know how to debug it. Some errors could not be debugged, so I often had to think about the problem in a different direction. So when faced with confusion, I had to test it. I could only solve my own mistakes because it was often difficult for others to understand my mistakes. Alas, it was really lonely to struggle alone like me. When I encountered confusion, I either gave up or persisted, but I didn't know how long I would persist. I was always afraid of wasting time like this.
 
At present, there is still a problem that has not been solved: if the time is adjusted unreasonably, the original time must be restored and there will be an error prompt.
For example, if the time is: 2012-2-34, if you change it to this date, there must be an error message and the time must be cancelled.
 
#include
#include "MyFuntion.h" //Custom header file
 
 
//Scan whether these buttons are pressed
void Keycan()
{
  if(Key1==0) //Enter the time adjustment state
  {
     Delay();
     if(Key1==0) //Stop timer T0 and start timer T1 
     {
    ButtonBeep();
    SaveCurrentTime(); //When entering the time adjustment state, the current time must be saved. If the time adjustment is canceled, the time can be restored
        TR0=0; //Stop timer T0
TR1=1; //Start timer T1 
tempSecond=0;     
     }
     while(!Key1);
  }
 
  if(Key2==0) 
  {
     Delay();
     if(Key2==0) //Cancel the time adjustment and restore the actual time before the MCU adjustment
     {
    if(TR0==0) //Restore time in the time adjustment state
{
  ButtonBeep();
          CurrentTime(); 
}   
     }
     while(!Key2);
  }
 
  if(Key3==0) //Exit time adjustment state:
  {
     Delay();
     if(Key3==0)
     {
    ButtonBeep(); //Stop timer T1 and start timer T0 
        TR0=1; //Start timer T0
TR1=0; //Stop timer T1 
Write_Command(0x0c); //Set to display or not to display cursor   
     }
     while(!Key3);
  }
 
//When timer T0 stops, the following buttons can be operated
 
  if(TR0==0) //Timer T0 stops
  {
  if(Key4==0) // Key4 controls the cursor to move right
  {
     Delay();
     if(Key4==0)
     {
  AdjustCursorRight(); //Control the cursor to move right
ButtonBeep();
     }
     while(!Key4);
  }
 
  if(Key5==0) // Key5 controls the cursor to move left
  {
     Delay();
     if(Key5==0)
     {
  AdjustCursorLeft(); //Control the cursor to move left
ButtonBeep();
     }
     while(!Key5);
  }
 
  if(Key6==0) // increment
  {
     Delay();
     if(Key6==0)
     {
    UpDown=1;
ButtonSetTime();
ButtonBeep();
     }
     while(!Key6);
  }
  
  if(Key7==0) // decrement 
  {
     Delay();
     if(Key7==0)
     {
    UpDown=0;
ButtonSetTime();
            ButtonBeep();
     }
     while(!Key7);
  }
  }
 
}
 
 
void main()
{
   TMOD=0X01;
   EA=1;
   ET0=1;
   ET1=1;
   TR0=1; //Start timer T0
// TR1=0; //Stop timer T1, that is, temporarily do not start timer T1
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
   TH1=(65536-50000)/256;
   TL1=(65536-50000)%256;
 
   InitLCD(); //Initialize LCD
   Init_Table_YMD(table1); // LCD timetable year, month, day
   Init_Table_HMS(table2); // LCD timetable hours, minutes, seconds
   DisplayLCD_YMD(table1, 10); //LCD displays the year, month, and day
   DisplayLCD_HMS(table2, 8); //LCD displays time in hours, minutes and seconds
 
   while(1)
   {
      Keycan();
  if(t0==20)
  {
    t0=0;
IncreaseHMS(); // hours, minutes, seconds increase
  }
   }
   
 
 //Timer T0 realizes automatic timing
void LCD_Timer0() interrupt 1 using 0
{
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
 
   t0++;
}
 
 //Timer T1 realizes automatic timing to reduce the error caused by adjusting the time
void LCD_Timer1() interrupt 3 using 1
{
   TH1=(65536-50000)/256;
   TL1=(65536-50000)%256;
 
   t1++;
   if(t1==20)
   {
  t1=0;
  tempSecond++;
   }
}

Keywords:1602LCD Reference address:1602LCD digital electronic clock C language

Previous article:How to realize DS1302 real-time clock display on 1602 LCD
Next article:How to program a digital electronic clock using 1602 LCD

Latest Microcontroller Articles
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号