⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtc.c

📁 schduler for AVR mcu
💻 C
字号:
/*
------------------------------------------------------------------*-

   RTC.C (v1.00)

  ------------------------------------------------------------------
   
   LCD display  program (Test Version 1.0)


   COPYRIGHT
   ---------

   This code is from the book:

   PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont 
   [Pearson Education, 2001; ISBN: 0-201-33138-1].

   This code is copyright (c) 2001 by Michael J. Pont.
 
   --- Modefied by  sylva zhu to apply for  AVR Microcontroller .
   --- Ver 1.0  Sept 25th , 2006 .

-*------------------------------------------------------------------
*/

#include  <iom32.h>
#include  <inavr.h>
#include  <comp_a90.h>

#include  "RTC.h"
#include  "T_Lcd.h"
//
//
RTC_STRUCT  RTC;
//
unsigned char  RTCSECOND_L;
unsigned char  RTCSECOND_H;
unsigned char  RTCMINUTE_L;
unsigned char  RTCMINUTE_H;
unsigned char  RTCHOUR_L;
unsigned char  RTCHOUR_H;
unsigned char  RTCDAY_L;
unsigned char  RTCDAY_H;
unsigned char  RTCMONTH_L;
unsigned char  RTCMONTH_H;
unsigned char  RTCYEAR_LL;
unsigned char  RTCYEAR_HH;
//
// Lookup table used to determine the highest date in a specific month
unsigned char __flash  MaxDate[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//
//------------------------------------------------------------------
//
void InitRTC(void)
{
  RTC.yearh=20;
  RTC.yearl=6;
  RTC.month=10;
  RTC.day=5;
  RTC.hour=8;
  RTC.minute=0;
  RTC.second=0;
}
//
//------------------------------------------------------------------
//   Called every 1000mS , 
void RTC_Update(void)
{
  unsigned char LeapYear = FALSE;
  static unsigned char blink=0;
  //
  //
  RTC.second++;
  
  if(RTC.second > 59)                         // if one minute
    {
        RTC.second = 0;                         // clear SECOND
        RTC.minute++;                           // increment MINUTE

        if(RTC.minute > 59)                     // if one hour
        {
            RTC.minute = 0;                     // clear MINUTE
            RTC.hour++;                         // increment HOUR

            if(RTC.hour > 23)                   // if one hour
            {
                RTC.hour = 0;                   // clear HOUR
                RTC.day++;                      // increment DAY
                //
                //---------------------------------------------------------------------------------------------//
                if(RTC.month == 2)              // if it's February
                  {
                    // check for leap year
                    if(!RTC.yearl)                        // if YEAR_LO = 0, (a century has passed)
                    {
                        if(!(RTC.yearh%4))                // check if YEAR_HI is divisible by 4
                            LeapYear = TRUE;                // then it is a leap year
                    }
                    else if(!(RTC.yearl%4) & (RTC.yearl != 0))  // else if YEAR_LO is divisible by 4 and not zero
                        LeapYear = TRUE;                    // then it's a leap year
                  }
                //---------------------------------------------------------------------------------------------//
                //
                if(RTC.day > (MaxDate[RTC.month] + LeapYear))   // if a whole month has passed
                {
                    RTC.day = 1;                // clear DAY
                    RTC.month++;                // increment MONTH

                    if(RTC.month > 12)          // if one year
                    {
                        RTC.month = 1;          // clear MONTH
                        RTC.yearl++;          // increment YEAR_LO

                        if(RTC.yearl > 99)    // if one century
                        {
                            RTC.yearl = 0;    // clear YEAR_LO
                            RTC.yearh++;      // increment YEAR_HI

                            if(RTC.yearh > 99)    // if 100 centuries
                                RTC.yearh = 0;    // the AVR is still going strong :)
                        }
                    }
                }
            }
        }
    }
 RTCSECOND_L=RTC.second%10;
 RTCSECOND_H=RTC.second%100/10;
 //
 RTCMINUTE_L=RTC.minute%10;
 RTCMINUTE_H=RTC.minute%100/10;
 //
 RTCHOUR_L=RTC.hour%10;
 RTCHOUR_H=RTC.hour%100/10;
 //
 RTCDAY_L=RTC.day%10;
 RTCDAY_H=RTC.day%100/10;
 //
 RTCMONTH_L=RTC.month%10;
 RTCMONTH_H=RTC.month%100/10;
 //
 RTCYEAR_LL=RTC.yearl%10;
 RTCYEAR_HH=RTC.yearl%100/10;
 //
 //
 if( blink%2 ==0 )
   {
     ShowChar(29,':');
   }
 else
   {
     ShowChar(29,' ');
   }
 //
 if( blink%2 ==0 )
   {
     ShowChar(25,':');
   }
 else
   {
     ShowChar(25,' ');
   }
 //
 blink++;
 //
 ShowChar(30,RTCSECOND_H+0x30);
 ShowChar(31,RTCSECOND_L+0x30);
 ShowChar(32,'S');
 //
 ShowChar(26,RTCMINUTE_H+0x30);
 ShowChar(27,RTCMINUTE_L+0x30);
 ShowChar(28,'M');
 //
 ShowChar(22,RTCHOUR_H+0x30);
 ShowChar(23,RTCHOUR_L+0x30);
 ShowChar(24,'H');
 /*
 ShowChar(25,+0x30);
 ShowChar(26,RTCYEAR_LL+0x30);
 ShowChar(27,'Y');
 ShowChar(28,':');
 ShowChar(29,RTCMONTH_H+0x30);
 ShowChar(30,RTCMONTH_L+0x30);
 ShowChar(31,'M');
 ShowChar(32,':');
 ShowChar(33,RTCDAY_H+0x30);
 ShowChar(34,RTCDAY_L+0x30);
 ShowChar(35,'D');
 ShowChar(36,' ');
 ShowChar(37,' ');
 ShowChar(38,' ');
 ShowChar(39,' ');
 */
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -