Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

rtc.c

Go to the documentation of this file.
00001 /*! \file rtc.c \brief Real-time clock function library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'rtc.c'
00005 // Title        : Real-time clock function library
00006 // Author       : Pascal Stang - Copyright (C) 2002
00007 // Created      : 5/10/2002
00008 // Revised      : 9/30/2002
00009 // Version      : 0.6
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021 
00022 #ifndef WIN32
00023     #include <avr/io.h>
00024     #include <avr/signal.h>
00025     #include <avr/interrupt.h>
00026     #include <avr/pgmspace.h>
00027 #endif
00028 
00029 #include "global.h"
00030 // include timer support
00031 #ifdef __AVR_ATmega128__
00032     #include "timer128.h"
00033 #else
00034     #include "timer.h"
00035 #endif
00036 // include rtc header
00037 #include "rtc.h"
00038 
00039 // Program ROM constants
00040 static char __attribute__ ((progmem)) MonthDayTable[] = {31,28,31,30,31,30,31,31,30,31,30,31};
00041 
00042 // Global variables
00043 // time registers
00044 RtcTimeType RtcTime;
00045 
00046 void rtcInit(void)
00047 {
00048     // set up timer for RTC operation
00049     // initialize real-time registers
00050     RtcTime.totaltics = 0;
00051     RtcTime.tics = 0;
00052     RtcTime.seconds = 0;
00053     RtcTime.minutes = 0;
00054     RtcTime.hours = 0;
00055     RtcTime.day = 1;
00056     RtcTime.month = 1;
00057     RtcTime.year = 2000;
00058 
00059     // select the correct RTC timer based on bit defines
00060     #ifdef AS2
00061         // use timer2 for most AVRs
00062         // initialize timer 2
00063         timer2Init();
00064         // count with 32.768KHz/8
00065         timer2SetPrescaler(TIMER_CLK_DIV8);
00066         // switch to asynchronous input (32KHz crystal)
00067         sbi(ASSR, AS2);
00068         // attach service to real-time clock interrupt
00069         // rtcService() will be called at ((32768/8)/256) = 16Hz
00070         timerAttach(TIMER2OVERFLOW_INT, rtcService);
00071     #else
00072     #ifdef AS0
00073         // use timer0 for ATmega103, ATmega128
00074         // initialize timer 0
00075         timer0Init();
00076         // count with 32.768KHz/8
00077         timer0SetPrescaler(TIMER_CLK_DIV8);
00078         // switch to asynchronous input (32KHz crystal)
00079         sbi(ASSR, AS0);
00080         // attach service to real-time clock interrupt
00081         // rtcService() will be called at ((32768/8)/256) = 16Hz
00082         timerAttach(TIMER0OVERFLOW_INT, rtcService);
00083     #endif
00084     #endif
00085 }
00086 
00087 void rtcService(void)
00088 {
00089     // update real-time clock registers
00090     RtcTime.totaltics++;
00091     RtcTime.tics++;
00092     // check for overflows
00093     if(RtcTime.tics == 16)                          // tics
00094     {
00095         RtcTime.tics = 0;
00096         RtcTime.seconds++;                          // increment seconds
00097         if(RtcTime.seconds > 59)                    // check seconds overflow
00098         {
00099             RtcTime.seconds -= 60;
00100             RtcTime.minutes++;                      // increment minutes
00101             if(RtcTime.minutes > 59)                // check minutes overflow
00102             {
00103                 RtcTime.minutes -= 60;
00104                 RtcTime.hours++;                    // increment hours
00105                 if(RtcTime.hours > 23)              // check hours overflow
00106                 {
00107                     RtcTime.hours -= 24;
00108                     RtcTime.day++;                  // increment days
00109                     // check days overflow
00110                     if(RtcTime.day == pgm_read_byte(&MonthDayTable[RtcTime.month-1]))
00111                     {
00112                         RtcTime.day = 1;
00113                         RtcTime.month++;            // increment months
00114                         if(RtcTime.month == 13)     // check months overflow
00115                         {
00116                             RtcTime.month = 1;
00117                             RtcTime.year++;         // increment years
00118                         }
00119                     }
00120                 }
00121             }
00122         }
00123     }
00124 }
00125 
00126 RtcTimeType* rtcGetTime(void)
00127 {
00128     // return the real-time clock data
00129     return &RtcTime;
00130 }
00131 

Generated on Tue Sep 20 03:11:43 2005 for Procyon AVRlib by  doxygen 1.4.2