?lang_form?
?lang_select?
?lang_submit?
?lang_endform?
{HEADER END}
{FILE START}
library
?curdirlinks? - Rev 6
?prevdifflink? - Blame - ?getfile?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Procyon AVRlib: rtc.c Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>rtc.c</h1><a href="rtc_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file rtc.c \brief Real-time clock function library. */</span>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name : 'rtc.c'</span>
00005 <span class="comment">// Title : Real-time clock function library</span>
00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002</span>
00007 <span class="comment">// Created : 5/10/2002</span>
00008 <span class="comment">// Revised : 9/30/2002</span>
00009 <span class="comment">// Version : 0.6</span>
00010 <span class="comment">// Target MCU : Atmel AVR Series</span>
00011 <span class="comment">// Editor Tabs : 4</span>
00012 <span class="comment">//</span>
00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>
00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>
00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>
00016 <span class="comment">//</span>
00017 <span class="comment">// This code is distributed under the GNU Public License</span>
00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
00019 <span class="comment">//</span>
00020 <span class="comment">//*****************************************************************************</span>
00021
00022 <span class="preprocessor">#ifndef WIN32</span>
00023 <span class="preprocessor"></span><span class="preprocessor"> #include <avr/io.h></span>
00024 <span class="preprocessor"> #include <avr/interrupt.h></span>
00025 <span class="preprocessor"> #include <avr/pgmspace.h></span>
00026 <span class="preprocessor">#endif</span>
00027 <span class="preprocessor"></span>
00028 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
00029 <span class="comment">// include timer support</span>
00030 <span class="preprocessor">#ifdef __AVR_ATmega128__</span>
00031 <span class="preprocessor"></span><span class="preprocessor"> #include "<a class="code" href="timer128_8h.html">timer128.h</a>"</span>
00032 <span class="preprocessor">#else</span>
00033 <span class="preprocessor"></span><span class="preprocessor"> #include "<a class="code" href="timer_8h.html">timer.h</a>"</span>
00034 <span class="preprocessor">#endif</span>
00035 <span class="preprocessor"></span><span class="comment">// include rtc header</span>
00036 <span class="preprocessor">#include "<a class="code" href="rtc_8h.html">rtc.h</a>"</span>
00037
00038 <span class="comment">// Program ROM constants</span>
00039 <span class="keyword">static</span> <span class="keywordtype">char</span> __attribute__ ((progmem)) MonthDayTable[] = {31,28,31,30,31,30,31,31,30,31,30,31};
00040
00041 <span class="comment">// Global variables</span>
00042 <span class="comment">// time registers</span>
00043 RtcTimeType RtcTime;
00044
00045 <span class="keywordtype">void</span> rtcInit(<span class="keywordtype">void</span>)
00046 {
00047 <span class="comment">// set up timer for RTC operation</span>
00048 <span class="comment">// initialize real-time registers</span>
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 <span class="comment">// select the correct RTC timer based on bit defines</span>
00059 <span class="preprocessor"> #ifdef AS2</span>
00060 <span class="preprocessor"></span> <span class="comment">// use timer2 for most AVRs</span>
00061 <span class="comment">// initialize timer 2</span>
00062 timer2Init();
00063 <span class="comment">// count with 32.768KHz/8</span>
00064 <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga17">TIMER_CLK_DIV8</a>);
00065 <span class="comment">// switch to asynchronous input (32KHz crystal)</span>
00066 sbi(ASSR, AS2);
00067 <span class="comment">// attach service to real-time clock interrupt</span>
00068 <span class="comment">// rtcService() will be called at ((32768/8)/256) = 16Hz</span>
00069 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER2OVERFLOW_INT, rtcService);
00070 <span class="preprocessor"> #else</span>
00071 <span class="preprocessor"></span><span class="preprocessor"> #ifdef AS0</span>
00072 <span class="preprocessor"></span> <span class="comment">// use timer0 for ATmega103, ATmega128</span>
00073 <span class="comment">// initialize timer 0</span>
00074 <a class="code" href="group__timer.html#ga2">timer0Init</a>();
00075 <span class="comment">// count with 32.768KHz/8</span>
00076 <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga17">TIMER_CLK_DIV8</a>);
00077 <span class="comment">// switch to asynchronous input (32KHz crystal)</span>
00078 sbi(ASSR, AS0);
00079 <span class="comment">// attach service to real-time clock interrupt</span>
00080 <span class="comment">// rtcService() will be called at ((32768/8)/256) = 16Hz</span>
00081 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER0OVERFLOW_INT, rtcService);
00082 <span class="preprocessor"> #endif</span>
00083 <span class="preprocessor"></span><span class="preprocessor"> #endif</span>
00084 <span class="preprocessor"></span>}
00085
00086 <span class="keywordtype">void</span> rtcService(<span class="keywordtype">void</span>)
00087 {
00088 <span class="comment">// update real-time clock registers</span>
00089 RtcTime.totaltics++;
00090 RtcTime.tics++;
00091 <span class="comment">// check for overflows</span>
00092 <span class="keywordflow">if</span>(RtcTime.tics == 16) <span class="comment">// tics</span>
00093 {
00094 RtcTime.tics = 0;
00095 RtcTime.seconds++; <span class="comment">// increment seconds</span>
00096 <span class="keywordflow">if</span>(RtcTime.seconds > 59) <span class="comment">// check seconds overflow</span>
00097 {
00098 RtcTime.seconds -= 60;
00099 RtcTime.minutes++; <span class="comment">// increment minutes</span>
00100 <span class="keywordflow">if</span>(RtcTime.minutes > 59) <span class="comment">// check minutes overflow</span>
00101 {
00102 RtcTime.minutes -= 60;
00103 RtcTime.hours++; <span class="comment">// increment hours</span>
00104 <span class="keywordflow">if</span>(RtcTime.hours > 23) <span class="comment">// check hours overflow</span>
00105 {
00106 RtcTime.hours -= 24;
00107 RtcTime.day++; <span class="comment">// increment days</span>
00108 <span class="comment">// check days overflow</span>
00109 <span class="keywordflow">if</span>(RtcTime.day == pgm_read_byte(&MonthDayTable[RtcTime.month-1]))
00110 {
00111 RtcTime.day = 1;
00112 RtcTime.month++; <span class="comment">// increment months</span>
00113 <span class="keywordflow">if</span>(RtcTime.month == 13) <span class="comment">// check months overflow</span>
00114 {
00115 RtcTime.month = 1;
00116 RtcTime.year++; <span class="comment">// increment years</span>
00117 }
00118 }
00119 }
00120 }
00121 }
00122 }
00123 }
00124
00125 RtcTimeType* rtcGetTime(<span class="keywordtype">void</span>)
00126 {
00127 <span class="comment">// return the real-time clock data</span>
00128 <span class="keywordflow">return</span> &RtcTime;
00129 }
00130
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|
{FILE END}
{FOOTER START}
Powered by WebSVN v2.8.3