⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtc_8c-source.html

📁 国外牛人公开的AVR代码
💻 HTML
字号:
<!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&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;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 &lt;avr/io.h&gt;</span>00024 <span class="preprocessor">    #include &lt;avr/signal.h&gt;</span>00025 <span class="preprocessor">    #include &lt;avr/interrupt.h&gt;</span>00026 <span class="preprocessor">    #include &lt;avr/pgmspace.h&gt;</span>00027 <span class="preprocessor">#endif</span>00028 <span class="preprocessor"></span>00029 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00030 <span class="comment">// include timer support</span>00031 <span class="preprocessor">#ifdef __AVR_ATmega128__</span>00032 <span class="preprocessor"></span><span class="preprocessor">    #include "<a class="code" href="timer128_8h.html">timer128.h</a>"</span>00033 <span class="preprocessor">#else</span>00034 <span class="preprocessor"></span><span class="preprocessor">    #include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00035 <span class="preprocessor">#endif</span>00036 <span class="preprocessor"></span><span class="comment">// include rtc header</span>00037 <span class="preprocessor">#include "<a class="code" href="rtc_8h.html">rtc.h</a>"</span>00038 00039 <span class="comment">// Program ROM constants</span>00040 <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};00041 00042 <span class="comment">// Global variables</span>00043 <span class="comment">// time registers</span>00044 RtcTimeType RtcTime;00045 00046 <span class="keywordtype">void</span> rtcInit(<span class="keywordtype">void</span>)00047 {00048     <span class="comment">// set up timer for RTC operation</span>00049     <span class="comment">// initialize real-time registers</span>00050     RtcTime.totaltics = 0;00051     RtcTime.tics = 0;00052     RtcTime.seconds = 0;00053     RtcTime.minutes = 0;00054     RtcTime.hours = 0;00055     RtcTime.day = 1;00056     RtcTime.month = 1;00057     RtcTime.year = 2000;00058 00059     <span class="comment">// select the correct RTC timer based on bit defines</span>00060 <span class="preprocessor">    #ifdef AS2</span>00061 <span class="preprocessor"></span>        <span class="comment">// use timer2 for most AVRs</span>00062         <span class="comment">// initialize timer 2</span>00063         timer2Init();00064         <span class="comment">// count with 32.768KHz/8</span>00065         <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga17">TIMER_CLK_DIV8</a>);00066         <span class="comment">// switch to asynchronous input (32KHz crystal)</span>00067         sbi(ASSR, AS2);00068         <span class="comment">// attach service to real-time clock interrupt</span>00069         <span class="comment">// rtcService() will be called at ((32768/8)/256) = 16Hz</span>00070         <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER2OVERFLOW_INT, rtcService);00071 <span class="preprocessor">    #else</span>00072 <span class="preprocessor"></span><span class="preprocessor">    #ifdef AS0</span>00073 <span class="preprocessor"></span>        <span class="comment">// use timer0 for ATmega103, ATmega128</span>00074         <span class="comment">// initialize timer 0</span>00075         <a class="code" href="group__timer.html#ga2">timer0Init</a>();00076         <span class="comment">// count with 32.768KHz/8</span>00077         <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga17">TIMER_CLK_DIV8</a>);00078         <span class="comment">// switch to asynchronous input (32KHz crystal)</span>00079         sbi(ASSR, AS0);00080         <span class="comment">// attach service to real-time clock interrupt</span>00081         <span class="comment">// rtcService() will be called at ((32768/8)/256) = 16Hz</span>00082         <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER0OVERFLOW_INT, rtcService);00083 <span class="preprocessor">    #endif</span>00084 <span class="preprocessor"></span><span class="preprocessor">    #endif</span>00085 <span class="preprocessor"></span>}00086 00087 <span class="keywordtype">void</span> rtcService(<span class="keywordtype">void</span>)00088 {00089     <span class="comment">// update real-time clock registers</span>00090     RtcTime.totaltics++;00091     RtcTime.tics++;00092     <span class="comment">// check for overflows</span>00093     <span class="keywordflow">if</span>(RtcTime.tics == 16)                          <span class="comment">// tics</span>00094     {00095         RtcTime.tics = 0;00096         RtcTime.seconds++;                          <span class="comment">// increment seconds</span>00097         <span class="keywordflow">if</span>(RtcTime.seconds &gt; 59)                    <span class="comment">// check seconds overflow</span>00098         {00099             RtcTime.seconds -= 60;00100             RtcTime.minutes++;                      <span class="comment">// increment minutes</span>00101             <span class="keywordflow">if</span>(RtcTime.minutes &gt; 59)                <span class="comment">// check minutes overflow</span>00102             {00103                 RtcTime.minutes -= 60;00104                 RtcTime.hours++;                    <span class="comment">// increment hours</span>00105                 <span class="keywordflow">if</span>(RtcTime.hours &gt; 23)              <span class="comment">// check hours overflow</span>00106                 {00107                     RtcTime.hours -= 24;00108                     RtcTime.day++;                  <span class="comment">// increment days</span>00109                     <span class="comment">// check days overflow</span>00110                     <span class="keywordflow">if</span>(RtcTime.day == pgm_read_byte(&amp;MonthDayTable[RtcTime.month-1]))00111                     {00112                         RtcTime.day = 1;00113                         RtcTime.month++;            <span class="comment">// increment months</span>00114                         <span class="keywordflow">if</span>(RtcTime.month == 13)     <span class="comment">// check months overflow</span>00115                         {00116                             RtcTime.month = 1;00117                             RtcTime.year++;         <span class="comment">// increment years</span>00118                         }00119                     }00120                 }00121             }00122         }00123     }00124 }00125 00126 RtcTimeType* rtcGetTime(<span class="keywordtype">void</span>)00127 {00128     <span class="comment">// return the real-time clock data</span>00129     <span class="keywordflow">return</span> &amp;RtcTime;00130 }00131 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Oct 24 16:03:55 2005 for Procyon AVRlib by&nbsp;<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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -