Main Page   Compound List   File List   Compound Members   File Members  

rtc.c

Go to the documentation of this file.
00001 
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              : 7/12/2002
00009 // Version              : 0.5
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 3
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 //***Changes by MG
00023 // line 87 changed [month-1] to [RtcTime.month-] (was causing compiler error)
00024 #ifndef WIN32
00025         #include <io.h>
00026         #include <sig-avr.h>
00027         #include <interrupt.h>
00028         #include <progmem.h>
00029 #endif
00030 
00031 #include "global.h"
00032 #include "timer.h"
00033 #include "rtc.h"
00034 
00035 // Program ROM constants
00036 static char __attribute__ ((progmem)) MonthDayTable[] = {31,28,31,30,31,30,31,31,30,31,30,31};
00037 
00038 // Global variables
00039 // time registers
00040 RtcTimeType RtcTime;
00041 
00042 void rtcInit(void)
00043 {
00044         // set up timer for RTC operation
00045         // initialize real-time registers
00046         RtcTime.totaltics = 0;
00047         RtcTime.tics = 0;
00048         RtcTime.seconds = 0;
00049         RtcTime.minutes = 0;
00050         RtcTime.hours = 0;
00051         RtcTime.day = 1;
00052         RtcTime.month = 1;
00053         RtcTime.year = 2000;
00054 
00055         // initialize timer 2
00056         timer2Init();
00057         // count with 32.768KHz/8
00058         timer2SetPrescaler(TIMER_CLK_DIV8);
00059         // switch to asynchronous input (32KHz crystal)
00060         sbi(ASSR, AS2);
00061 
00062         // attach service to real-time clock interrupt
00063         // rtcService() will be called at ((32768/8)/256) = 16Hz
00064         timerAttach(TIMER2OVERFLOW_INT, rtcService);
00065 }
00066 
00067 void rtcService(void)
00068 {
00069         // update real-time clock registers
00070         RtcTime.totaltics++;
00071         RtcTime.tics++;
00072         // check for overflows
00073         if(RtcTime.tics == 16)                                                  // tics
00074         {
00075                 RtcTime.tics = 0;
00076                 RtcTime.seconds++;                                                      // increment seconds
00077                 if(RtcTime.seconds > 59)                                        // check seconds overflow
00078                 {
00079                         RtcTime.seconds -= 60;
00080                         RtcTime.minutes++;                                              // increment minutes
00081                         if(RtcTime.minutes > 59)                                // check minutes overflow
00082                         {
00083                                 RtcTime.minutes -= 60;
00084                                 RtcTime.hours++;                                                // increment hours
00085                                 if(RtcTime.hours > 23)                          // check hours overflow
00086                                 {
00087                                         RtcTime.hours -= 24;
00088                                         RtcTime.day++;                                          // increment days
00089                                         if(RtcTime.day == PRG_RDB(&MonthDayTable[RtcTime.month-1]))// check days overflow
00090                                         {
00091                                                 RtcTime.day = 1;
00092                                                 RtcTime.month++;                                // increment months
00093                                                 if(RtcTime.month == 13)         // check months overflow
00094                                                 {
00095                                                         RtcTime.month = 1;
00096                                                         RtcTime.year++;                 // increment years
00097                                                 }
00098                                         }
00099                                 }
00100                         }
00101                 }
00102         }
00103 }
00104 
00105 RtcTimeType* rtcGetTime(void)
00106 {
00107         // return the real-time clock data
00108         return &RtcTime;
00109 }
00110 

Generated at Fri Oct 25 15:36:38 2002 for avrlib by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001