Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file rtc.c \brief Real-time clock function library. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'rtc.c' |
||
5 | // Title : Real-time clock function library |
||
6 | // Author : Pascal Stang - Copyright (C) 2002 |
||
7 | // Created : 5/10/2002 |
||
8 | // Revised : 9/30/2002 |
||
9 | // Version : 0.6 |
||
10 | // Target MCU : Atmel AVR Series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
14 | // to be lacking in some functionality or documentation, or may not be fully |
||
15 | // tested. Nonetheless, you can expect most functions to work. |
||
16 | // |
||
17 | // This code is distributed under the GNU Public License |
||
18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
19 | // |
||
20 | //***************************************************************************** |
||
21 | |||
22 | #ifndef WIN32 |
||
23 | #include <avr/io.h> |
||
24 | #include <avr/interrupt.h> |
||
25 | #include <avr/pgmspace.h> |
||
26 | #endif |
||
27 | |||
28 | #include "global.h" |
||
29 | // include timer support |
||
30 | #ifdef __AVR_ATmega128__ |
||
31 | #include "timer128.h" |
||
32 | #else |
||
33 | #include "timer.h" |
||
34 | #endif |
||
35 | // include rtc header |
||
36 | #include "rtc.h" |
||
37 | |||
38 | // Program ROM constants |
||
39 | static char __attribute__ ((progmem)) MonthDayTable[] = {31,28,31,30,31,30,31,31,30,31,30,31}; |
||
40 | |||
41 | // Global variables |
||
42 | // time registers |
||
43 | RtcTimeType RtcTime; |
||
44 | |||
45 | void rtcInit(void) |
||
46 | { |
||
47 | // set up timer for RTC operation |
||
48 | // initialize real-time registers |
||
49 | RtcTime.totaltics = 0; |
||
50 | RtcTime.tics = 0; |
||
51 | RtcTime.seconds = 0; |
||
52 | RtcTime.minutes = 0; |
||
53 | RtcTime.hours = 0; |
||
54 | RtcTime.day = 1; |
||
55 | RtcTime.month = 1; |
||
56 | RtcTime.year = 2000; |
||
57 | |||
58 | // select the correct RTC timer based on bit defines |
||
59 | #ifdef AS2 |
||
60 | // use timer2 for most AVRs |
||
61 | // initialize timer 2 |
||
62 | timer2Init(); |
||
63 | // count with 32.768KHz/8 |
||
64 | timer2SetPrescaler(TIMER_CLK_DIV8); |
||
65 | // switch to asynchronous input (32KHz crystal) |
||
66 | sbi(ASSR, AS2); |
||
67 | // attach service to real-time clock interrupt |
||
68 | // rtcService() will be called at ((32768/8)/256) = 16Hz |
||
69 | timerAttach(TIMER2OVERFLOW_INT, rtcService); |
||
70 | #else |
||
71 | #ifdef AS0 |
||
72 | // use timer0 for ATmega103, ATmega128 |
||
73 | // initialize timer 0 |
||
74 | timer0Init(); |
||
75 | // count with 32.768KHz/8 |
||
76 | timer0SetPrescaler(TIMER_CLK_DIV8); |
||
77 | // switch to asynchronous input (32KHz crystal) |
||
78 | sbi(ASSR, AS0); |
||
79 | // attach service to real-time clock interrupt |
||
80 | // rtcService() will be called at ((32768/8)/256) = 16Hz |
||
81 | timerAttach(TIMER0OVERFLOW_INT, rtcService); |
||
82 | #endif |
||
83 | #endif |
||
84 | } |
||
85 | |||
86 | void rtcService(void) |
||
87 | { |
||
88 | // update real-time clock registers |
||
89 | RtcTime.totaltics++; |
||
90 | RtcTime.tics++; |
||
91 | // check for overflows |
||
92 | if(RtcTime.tics == 16) // tics |
||
93 | { |
||
94 | RtcTime.tics = 0; |
||
95 | RtcTime.seconds++; // increment seconds |
||
96 | if(RtcTime.seconds > 59) // check seconds overflow |
||
97 | { |
||
98 | RtcTime.seconds -= 60; |
||
99 | RtcTime.minutes++; // increment minutes |
||
100 | if(RtcTime.minutes > 59) // check minutes overflow |
||
101 | { |
||
102 | RtcTime.minutes -= 60; |
||
103 | RtcTime.hours++; // increment hours |
||
104 | if(RtcTime.hours > 23) // check hours overflow |
||
105 | { |
||
106 | RtcTime.hours -= 24; |
||
107 | RtcTime.day++; // increment days |
||
108 | // check days overflow |
||
109 | if(RtcTime.day == pgm_read_byte(&MonthDayTable[RtcTime.month-1])) |
||
110 | { |
||
111 | RtcTime.day = 1; |
||
112 | RtcTime.month++; // increment months |
||
113 | if(RtcTime.month == 13) // check months overflow |
||
114 | { |
||
115 | RtcTime.month = 1; |
||
116 | RtcTime.year++; // increment years |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | RtcTimeType* rtcGetTime(void) |
||
126 | { |
||
127 | // return the real-time clock data |
||
128 | return &RtcTime; |
||
129 | } |
||
130 |
Powered by WebSVN v2.8.3