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/interrupt.h>
00025     #include <avr/pgmspace.h>
00026 #endif
00027 
00028 #include "global.h"
00029 // include timer support
00030 #ifdef __AVR_ATmega128__
00031     #include "timer128.h"
00032 #else
00033     #include "timer.h"
00034 #endif
00035 // include rtc header
00036 #include "rtc.h"
00037 
00038 // Program ROM constants
00039 static char __attribute__ ((progmem)) MonthDayTable[] = {31,28,31,30,31,30,31,31,30,31,30,31};
00040 
00041 // Global variables
00042 // time registers
00043 RtcTimeType RtcTime;
00044 
00045 void rtcInit(void)
00046 {
00047     // set up timer for RTC operation
00048     // initialize real-time registers
00049     RtcTime.totaltics = 0;
00050     RtcTime.tics = 0;
00051     RtcTime.seconds = 0;
00052     RtcTime.minutes = 0;
00053     RtcTime.hours = 0;
00054     RtcTime.day = 1;
00055     RtcTime.month = 1;
00056     RtcTime.year = 2000;
00057 
00058     // select the correct RTC timer based on bit defines
00059     #ifdef AS2
00060         // use timer2 for most AVRs
00061         // initialize timer 2
00062         timer2Init();
00063         // count with 32.768KHz/8
00064         timer2SetPrescaler(TIMER_CLK_DIV8);
00065         // switch to asynchronous input (32KHz crystal)
00066         sbi(ASSR, AS2);
00067         // attach service to real-time clock interrupt
00068         // rtcService() will be called at ((32768/8)/256) = 16Hz
00069         timerAttach(TIMER2OVERFLOW_INT, rtcService);
00070     #else
00071     #ifdef AS0
00072         // use timer0 for ATmega103, ATmega128
00073         // initialize timer 0
00074         timer0Init();
00075         // count with 32.768KHz/8
00076         timer0SetPrescaler(TIMER_CLK_DIV8);
00077         // switch to asynchronous input (32KHz crystal)
00078         sbi(ASSR, AS0);
00079         // attach service to real-time clock interrupt
00080         // rtcService() will be called at ((32768/8)/256) = 16Hz
00081         timerAttach(TIMER0OVERFLOW_INT, rtcService);
00082     #endif
00083     #endif
00084 }
00085 
00086 void rtcService(void)
00087 {
00088     // update real-time clock registers
00089     RtcTime.totaltics++;
00090     RtcTime.tics++;
00091     // check for overflows
00092     if(RtcTime.tics == 16)                          // tics
00093     {
00094         RtcTime.tics = 0;
00095         RtcTime.seconds++;                          // increment seconds
00096         if(RtcTime.seconds > 59)                    // check seconds overflow
00097         {
00098             RtcTime.seconds -= 60;
00099             RtcTime.minutes++;                      // increment minutes
00100             if(RtcTime.minutes > 59)                // check minutes overflow
00101             {
00102                 RtcTime.minutes -= 60;
00103                 RtcTime.hours++;                    // increment hours
00104                 if(RtcTime.hours > 23)              // check hours overflow
00105                 {
00106                     RtcTime.hours -= 24;
00107                     RtcTime.day++;                  // increment days
00108                     // check days overflow
00109                     if(RtcTime.day == pgm_read_byte(&MonthDayTable[RtcTime.month-1]))
00110                     {
00111                         RtcTime.day = 1;
00112                         RtcTime.month++;            // increment months
00113                         if(RtcTime.month == 13)     // check months overflow
00114                         {
00115                             RtcTime.month = 1;
00116                             RtcTime.year++;         // increment years
00117                         }
00118                     }
00119                 }
00120             }
00121         }
00122     }
00123 }
00124 
00125 RtcTimeType* rtcGetTime(void)
00126 {
00127     // return the real-time clock data
00128     return &RtcTime;
00129 }
00130 

Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by  doxygen 1.4.2