00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
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 
00030 #ifdef __AVR_ATmega128__
00031     #include "timer128.h"
00032 #else
00033     #include "timer.h"
00034 #endif
00035 
00036 #include "rtc.h"
00037 
00038 
00039 static char __attribute__ ((progmem)) MonthDayTable[] = {31,28,31,30,31,30,31,31,30,31,30,31};
00040 
00041 
00042 
00043 RtcTimeType RtcTime;
00044 
00045 void rtcInit(void)
00046 {
00047     
00048     
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     
00059     #ifdef AS2
00060         
00061         
00062         timer2Init();
00063         
00064         timer2SetPrescaler(TIMER_CLK_DIV8);
00065         
00066         sbi(ASSR, AS2);
00067         
00068         
00069         timerAttach(TIMER2OVERFLOW_INT, rtcService);
00070     #else
00071     #ifdef AS0
00072         
00073         
00074         timer0Init();
00075         
00076         timer0SetPrescaler(TIMER_CLK_DIV8);
00077         
00078         sbi(ASSR, AS0);
00079         
00080         
00081         timerAttach(TIMER0OVERFLOW_INT, rtcService);
00082     #endif
00083     #endif
00084 }
00085 
00086 void rtcService(void)
00087 {
00088     
00089     RtcTime.totaltics++;
00090     RtcTime.tics++;
00091     
00092     if(RtcTime.tics == 16)                          
00093     {
00094         RtcTime.tics = 0;
00095         RtcTime.seconds++;                          
00096         if(RtcTime.seconds > 59)                    
00097         {
00098             RtcTime.seconds -= 60;
00099             RtcTime.minutes++;                      
00100             if(RtcTime.minutes > 59)                
00101             {
00102                 RtcTime.minutes -= 60;
00103                 RtcTime.hours++;                    
00104                 if(RtcTime.hours > 23)              
00105                 {
00106                     RtcTime.hours -= 24;
00107                     RtcTime.day++;                  
00108                     
00109                     if(RtcTime.day == pgm_read_byte(&MonthDayTable[RtcTime.month-1]))
00110                     {
00111                         RtcTime.day = 1;
00112                         RtcTime.month++;            
00113                         if(RtcTime.month == 13)     
00114                         {
00115                             RtcTime.month = 1;
00116                             RtcTime.year++;         
00117                         }
00118                     }
00119                 }
00120             }
00121         }
00122     }
00123 }
00124 
00125 RtcTimeType* rtcGetTime(void)
00126 {
00127     
00128     return &RtcTime;
00129 }
00130