×
单片机 > 单片机程序设计 > 详情

c51写的日历时钟程序3

发布时间:2020-06-08 发布时间:
|

*********************************************************************************************************
*                                       DETERMINE IF WE HAVE A LEAP YEAR
*
* Description : This function determines whether the 'year' passed as an argument is a leap year.
* Arguments     : year      is the year to check for leap year.
* Returns       : TRUE      if 'year' is a leap year.
*                 FALSE     if 'year' is NOT a leap year.
*********************************************************************************************************
*/
#if CLK_DATE_EN
static    BOOL    ClkIsLeapYear(UINT year)
{
      if (!(year % 4) && (year % 100) || !(year % 400)) 
       {
          return TRUE;
       } 
      else 
       {
          return (FALSE);
       }
}
#endif
/*
*********************************************************************************************************
*                                              SET DATE ONLY
*
* Description : Set the date of the time-of-day clock
* Arguments     : month       is the desired month (1..12)
*                 day         is the desired day     (1..31)
*                 year        is the desired year    (CLK_TS_BASE_YEAR .. CLK_TS_BASE_YEAR+63)
* Returns       : None.
* Notes         : It is assumed that you are specifying a correct date (i.e. there is no range checking
*                 done by this function).
*********************************************************************************************************
*/
#if    CLK_DATE_EN
void    Clk_set_date (UCHAR month, UCHAR day, UINT year)
{
      ENTER_CRITICAL();                         
      ClkMonth = month;
      ClkDay     = day;
      ClkYear    = year;
      ClkUpdateDOW();                             
      EXIT_CRITICAL();                         
}
#endif
/*
*********************************************************************************************************
*                                            SET DATE AND TIME
*
* Description : Set the date and time of the time-of-day clock
* Arguments     : month       is the desired month     (1..12)
*                 day         is the desired day       (1..31)
*                 year        is the desired year      (2xxx)
*                 hr          is the desired hour      (0..23)
*                 min         is the desired minutes (0..59)
*                 sec         is the desired seconds (0..59)
* Returns       : None.
* Notes         : It is assumed that you are specifying a correct date and time (i.e. there is no range
*                 checking done by this function).
*********************************************************************************************************
*/

#if    CLK_DATE_EN
void    Clk_set_date_time (UCHAR month, UCHAR day, UINT year, UCHAR hr, UCHAR min, UCHAR sec)
{
      ENTER_CRITICAL();                           /* Gain exclusive access to time-of-day clock           */
      ClkMonth = month;
      ClkDay     = day;
      ClkYear    = year;
      ClkHr      = hr;
      ClkMin     = min;
      ClkSec     = sec;
      ClkUpdateDOW();                                /* Compute the day of the week (i.e. Sunday ...)        */
      EXIT_CRITICAL();                            /* Release access to time-of-day clock                  */
}
#endif
/*
*********************************************************************************************************
*                                              UPDATE THE DATE
*
* Description : This function is called to update the date (i.e. month, day and year)
* Arguments     : None.
* Returns       : None.
* Notes         : This function updates ClkDay, ClkMonth, ClkYear and ClkDOW.
*********************************************************************************************************
*/
#if CLK_DATE_EN
static    void    ClkUpdateDate (void)
{
      BOOL newmonth;
    
      newmonth = TRUE;
      if (ClkDay >= ClkMonthTbl[ClkMonth].MonthDays) {    /* Last day of the month?                          */
          if (ClkMonth == 2) {                            /* Is this February?                               */
              if (ClkIsLeapYear(ClkYear) == TRUE) {       /* Yes, Is this a leap year?                       */
                  if (ClkDay >= 29) {                     /* Yes, Last day in february?                      */
                      ClkDay = 1;                         /* Yes, Set to 1st day in March                    */
                  } else {
                      ClkDay++;
                      newmonth = FALSE;
                  }
              } else {
                  ClkDay = 1;
              }
          } else {
              ClkDay = 1;
          }
      } else {
          ClkDay++;
          newmonth = FALSE;
      }
      if (newmonth == TRUE) {                        /* See if we have completed a month                     */
          if (ClkMonth >= 12) {                      /* Yes, Is this december ?                              */
              ClkMonth = 1;                          /* Yes, set month to january...                         */
              ClkYear++;                             /*        ...we have a new year!                          */
          } else {
              ClkMonth++;                            /* No,    increment the month                             */
          }
      }
      ClkUpdateDOW();                                /* Compute the day of the week (i.e. Sunday ...)        */
}
#endif
/*
*********************************************************************************************************
*                                           COMPUTE DAY-OF-WEEK
*
* Description : This function computes the day of the week (0 == Sunday) based on the current month,
*                 day and year.
* Arguments     : None.
* Returns       : None.
* Notes         : - This function updates ClkDOW.
*                 - This function is called by ClkUpdateDate().
*********************************************************************************************************
*/
#if CLK_DATE_EN
static    void    ClkUpdateDOW (void)
{
      UINT dow;
    
      dow = ClkDay + ClkMonthTbl[ClkMonth].MonthVal;
      if (ClkMonth < 3)
      {
          if (ClkIsLeapYear(ClkYear)) 
          {
              dow--;
          }
      }
      dow      += ClkYear + (ClkYear / 4);
      dow      += (ClkYear / 400) - (ClkYear / 100);
      dow      %= 7;
      ClkDOW    = dow;
}
#endif

//@@***********************************************************
//
//     功能:    给定的时间与当前时间比较
//     函数:    int Cmp_now_time(UCHAR hr, UCHAR min, UCHAR sec)
//     语言:    C
//     输入:         
//     输出:    0     相等
//            >0    给定时间大于当前时间
//            <0    给定时间小于当前时间 
//@@***********************************************************
//------------------------------------------------------------- 
int Cmp_now_time(UCHAR hr, UCHAR min, UCHAR sec)
{
     if(ClkHr!=hr)
       return(hr-ClkHr);
     else if(ClkMin!= min) 
       return(min-ClkMin);   
     else
       return(sec-ClkSec);    
}

//@@***********************************************************
//
//     功能:    给定的时间与当前时间比较
//     函数:    int Cmp_now_date_time(UCHAR month, UCHAR day, UINT year, UCHAR hr, UCHAR min, UCHAR sec)
//     语言:    C
//     输入:         
//     输出:    0     相等
//            >0    给定时间大于当前时间
//            <0    给定时间小于当前时间 
//@@***********************************************************
//------------------------------------------------------------- 
#if CLK_DATE_EN
int Cmp_now_date_time(UCHAR month, UCHAR day, UINT year, UCHAR hr, UCHAR min, UCHAR sec)
{
     if(ClkYear!=year)
         return(year-ClkYear);
     else if(ClkMonth!=month)
      return(month-ClkMonth);
     else if(ClkDay!=day)
      return(day-ClkDay);
     else if(ClkHr!=hr)
         return(hr-ClkHr);
     else if(ClkMin!= min) 
         return(min-ClkMin);   
     else
         return(sec-ClkSec);    
}
#endif


#endif





『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32中断向量表的位置.重定向