DS1302 digital tube display time and date adjustable C language programming

Publisher:Huixin8888Latest update time:2016-10-27 Source: eefocusKeywords:DS1302 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include <DS1302.h>
//define common anode font code 0123456789-
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
unsigned char time[]={0,0,0,0,0,0,0,0};//used to store time
unsigned char date[]={0,0,0,0,0,0,0,0};//used to store date
SYSTEMTIME time1; //A structure named SYSTEMTIME has been defined in the ds1302.h file. Here we define a variable named
                     //time in the SYSTEMTIME structure        
void delay(unsigned char i) //delay subroutine  
{
  unsigned char j;
  while((i--)!=0)
  {
    for(j=123;j>0;j--);
  }
}
unsigned char button_time(n,x,y) //Clock adjustment subroutine
unsigned char n,x,y;
{
    if(P1_7==0)
    {
        delay(50);
        if(P1_7==0)
     {
         n++;
            if(n==x)
         n=0;
            while(P1_7==0);
     }
    }

    if(P1_5==0)
    {
        delay(50);
        if(P1_5==0)
     {
            if(n==0)
         n=y;
         else
            n--;      
            while(P1_5==0);
        }
    }
    return n;
}

unsigned char button_date(n,x,y) //Date adjustment subroutine
unsigned char n,x,y;
{
    if(P1_7==0)
    {
        delay(50);
        if(P1_7==0)
     {
         n++;
            if(n==x)
         n=1;
            while(P1_7==0);
     }
    }

    if(P1_5==0)
    {
        delay(50);
        if(P1_5==0)
     {
            if(n==1)
         n=y;
         else
            n--;      
            while(P1_5==0);
        }
    }
    return n;
}

void display(second10,second1,minute10,minute1,hour10,hour1) //display subroutine
unsigned char second10,second1,minute10,minute1,hour10,hour1;  
{
  P2=0xfe;
  P0=dispcode[second10]; //display the tens digit of seconds
  delay(1);

  P2=0xfd;
  P0=dispcode[second1]; //Display the units digit of seconds
  delay(1);

  P2=0xfb;
  P0=dispcode[10]; //display "-"
  delay(1);

  P2=0xf7;
  P0=dispcode[minute10]; //display the tens digit of minute
  delay(1);

  P2=0xef;
  P0=dispcode[minute1]; //display the unit digit of minute
  delay(1);

  P2=0xdf;
  P0=dispcode[10]; //display "-"
  delay(1);

  P2=0xbf; P0=dispcode[hour10]; //   delay(1)
  for the displayed tens digit ;

  P2=0x7f;
  P0=dispcode[hour1]; //the unit digit of the display
  delay(1);

void main()
{  
   unsigned char flag;
   Initial_DS1302(); //Initialize the DS1302 clock chip,   
   while(1)           
   {
       DS1302_GetTime(&time1); //Read time parameters            
       time[5]=(time1.Second)%10; //Store the units digit of the second into time[5]
       time[4]=(time1.Second)/10; //Store the tens digit of the second into time[4]
       time[3]=(time1.Minute)%10; //Store the units digit of the minute into time[3]
       time[2]=(time1.Minute)/10; //Store the tens digit of the minute into time[2]
       time[1]=(time1.Hour)%10; //Store the units digit of the hour into time[1]
       time[0]=(time1.Hour)/10; //Store the tens digit of the hour into time[0]
    date[5]=(time1.Day)%10;
    date[4]=(time1.Day)/10;
    date[3]=(time1.Month)%10;
    date[2]=(time1.Month)/10;
    date[1]=(time1.Year)%10;
    date[0]=(time1.Year)/10;  

    if(P1_4==0) //If you press the Time Start key once, the clock starts to display the time normally, and press it again to display the date
    {
   delay(50);
   if(P1_4==0)
   {
    flag++;
    if(flag>1)
    {
     flag=0;
    }
   }
   while(P1_4==0);
  }
    if(P1_6==0) //If you press the Time Set key once, the date starts to be displayed, and press it again to enter the date and clock adjustment mode
    {
      delay(50);
   if(P1_6==0)
   {
    flag++;
    if(flag>7)
    {
     flag=0;
    }
   }
   while(P1_6==0);
  }

  switch(flag)
  {
   case 0:display(time[0],time[1],time[2],time[3],time[4],time[5]); //Call the sub-function display to display the data stored in the array time
       break;

   case 1:display(date[0],date[1],date[2],date[3],date[4],date[5]); //Call the sub-function display to display the data stored in the array date
       break;

   case 2:time1.Year=button_date(time1.Year,100,99);      //调整年
       DS1302_SetTime(0x8c,time1.Year);
       display(date[0],date[1],10,10,10,10);
       break;

   case 3:time1.Month=button_date(time1.Month,13,12);      //调整月
       DS1302_SetTime(0x88,time1.Month);
       display(10,10,date[2],date[3],10,10);
       break;

   case 4:time1.Day=button_date(time1.Day,32,31);       //调整日
       DS1302_SetTime(0x86,time1.Day);
       display(10,10,10,10,date[4],date[5]);
       break;

   case 5:time1.Hour=button_time(time1.Hour,24,23);         //调整时
       DS1302_SetTime(0x84,time1.Hour);
       display(time[0],time[1],10,10,10,10);
       break;

   case 6:time1.Minute=button_time(time1.Minute,60,59);        //调整分
       DS1302_SetTime(0x82,time1.Minute);
       display(10,10,time[2],time[3],10,10);
       break;

   case 7:time1.Second=button_time(time1.Second,60,59);        //调整秒
          DS1302_SetTime(0x80,time1.Second);
       display(10,10,10,10,time[4],time[5]);
       break;
  }   
 }
}

Keywords:DS1302 Reference address:DS1302 digital tube display time and date adjustable C language programming

Previous article:12864 LCD display serial driver demonstration C language programming
Next article:Calendar clock based on DS1302 (1602 LCD display DS1302 clock) C language programming

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号