1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00
arduinolibs/DS1307RTC_8cpp_source.html
Rhys Weatherley 17f4fb4873 Update docs
2012-06-18 11:03:07 +10:00

370 lines
41 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>ArduinoLibs: DS1307RTC.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generated by Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">ArduinoLibs</div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
<li id="searchli">
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&#160;List</span></a></li>
</ul>
</div>
<div class="header">
<div class="headertitle">
<div class="title">DS1307RTC.cpp</div> </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2012 Southern Storm Software, Pty Ltd.</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</span>
<a name="l00005"></a>00005 <span class="comment"> * copy of this software and associated documentation files (the &quot;Software&quot;),</span>
<a name="l00006"></a>00006 <span class="comment"> * to deal in the Software without restriction, including without limitation</span>
<a name="l00007"></a>00007 <span class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</span>
<a name="l00008"></a>00008 <span class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</span>
<a name="l00009"></a>00009 <span class="comment"> * Software is furnished to do so, subject to the following conditions:</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> * The above copyright notice and this permission notice shall be included</span>
<a name="l00012"></a>00012 <span class="comment"> * in all copies or substantial portions of the Software.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS</span>
<a name="l00015"></a>00015 <span class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<a name="l00016"></a>00016 <span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<a name="l00017"></a>00017 <span class="comment"> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<a name="l00018"></a>00018 <span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span>
<a name="l00019"></a>00019 <span class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</span>
<a name="l00020"></a>00020 <span class="comment"> * DEALINGS IN THE SOFTWARE.</span>
<a name="l00021"></a>00021 <span class="comment"> */</span>
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <span class="preprocessor">#include &quot;DS1307RTC.h&quot;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &quot;../I2C/I2CMaster.h&quot;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#if defined(ARDUINO) &amp;&amp; ARDUINO &gt;= 100</span>
<a name="l00026"></a>00026 <span class="preprocessor"></span><span class="preprocessor">#include &lt;Arduino.h&gt;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#else</span>
<a name="l00028"></a>00028 <span class="preprocessor"></span><span class="preprocessor">#include &lt;WProgram.h&gt;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#endif</span>
<a name="l00030"></a>00030 <span class="preprocessor"></span>
<a name="l00054"></a>00054 <span class="comment">// I2C address of the RTC chip (7-bit).</span>
<a name="l00055"></a>00055 <span class="preprocessor">#define DS1307_I2C_ADDRESS 0x68</span>
<a name="l00056"></a>00056 <span class="preprocessor"></span>
<a name="l00057"></a>00057 <span class="comment">// Registers.</span>
<a name="l00058"></a>00058 <span class="preprocessor">#define DS1307_SECOND 0x00</span>
<a name="l00059"></a>00059 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_MINUTE 0x01</span>
<a name="l00060"></a>00060 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_HOUR 0x02</span>
<a name="l00061"></a>00061 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_DAY_OF_WEEK 0x03</span>
<a name="l00062"></a>00062 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_DATE 0x04</span>
<a name="l00063"></a>00063 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_MONTH 0x05</span>
<a name="l00064"></a>00064 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_YEAR 0x06</span>
<a name="l00065"></a>00065 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_CONTROL 0x07</span>
<a name="l00066"></a>00066 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_NVRAM 0x08</span>
<a name="l00067"></a>00067 <span class="preprocessor"></span>
<a name="l00068"></a>00068 <span class="comment">// Alarm storage at the end of the RTC&#39;s NVRAM.</span>
<a name="l00069"></a>00069 <span class="preprocessor">#define DS1307_ALARM_SIZE 3</span>
<a name="l00070"></a>00070 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_ALARMS (64 - RTC::ALARM_COUNT * DS1307_ALARM_SIZE - 1)</span>
<a name="l00071"></a>00071 <span class="preprocessor"></span><span class="preprocessor">#define DS1307_ALARM_MAGIC 63</span>
<a name="l00072"></a>00072 <span class="preprocessor"></span>
<a name="l00083"></a><a class="code" href="classDS1307RTC.html#a092805d75bea323dc4be062638bff866">00083</a> <a class="code" href="classDS1307RTC.html#a092805d75bea323dc4be062638bff866" title="Attaches to a realtime clock slave device on bus.">DS1307RTC::DS1307RTC</a>(<a class="code" href="classI2CMaster.html" title="Abstract base class for I2C master implementations.">I2CMaster</a> &amp;bus, uint8_t oneHzPin)
<a name="l00084"></a>00084 : _bus(&amp;bus)
<a name="l00085"></a>00085 , _oneHzPin(oneHzPin)
<a name="l00086"></a>00086 , prevOneHz(false)
<a name="l00087"></a>00087 , _isRealTime(true)
<a name="l00088"></a>00088 {
<a name="l00089"></a>00089 <span class="comment">// Make sure the CH bit in register 0 is off or the clock won&#39;t update.</span>
<a name="l00090"></a>00090 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00091"></a>00091 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_SECOND);
<a name="l00092"></a>00092 <span class="keywordflow">if</span> (_bus-&gt;<a class="code" href="classI2CMaster.html#a4e5f1a1a4c2242699be5a35fc4872fde" title="Starts a read operation for count bytes by sending the start condition and the I2C control byte...">startRead</a>(DS1307_I2C_ADDRESS, 1)) {
<a name="l00093"></a>00093 uint8_t value = _bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>();
<a name="l00094"></a>00094 <span class="keywordflow">if</span> ((value &amp; 0x80) != 0)
<a name="l00095"></a>00095 writeRegister(DS1307_SECOND, value &amp; 0x7F);
<a name="l00096"></a>00096 } <span class="keywordflow">else</span> {
<a name="l00097"></a>00097 <span class="comment">// Did not get an acknowledgement from the RTC chip.</span>
<a name="l00098"></a>00098 _isRealTime = <span class="keyword">false</span>;
<a name="l00099"></a>00099 }
<a name="l00100"></a>00100
<a name="l00101"></a>00101 <span class="comment">// Turn on the 1 Hz square wave signal if required.</span>
<a name="l00102"></a>00102 <span class="keywordflow">if</span> (oneHzPin != 255 &amp;&amp; _isRealTime) {
<a name="l00103"></a>00103 pinMode(oneHzPin, INPUT);
<a name="l00104"></a>00104 digitalWrite(oneHzPin, HIGH);
<a name="l00105"></a>00105 writeRegister(DS1307_CONTROL, 0x10);
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107
<a name="l00108"></a>00108 <span class="comment">// Initialize the alarms in the RTC chip&#39;s NVRAM.</span>
<a name="l00109"></a>00109 <span class="keywordflow">if</span> (_isRealTime)
<a name="l00110"></a>00110 initAlarms();
<a name="l00111"></a>00111 }
<a name="l00112"></a>00112
<a name="l00118"></a><a class="code" href="classDS1307RTC.html#a6fec8ff71f33cc1a129eb0bd009600b0">00118</a> <span class="keywordtype">bool</span> <a class="code" href="classDS1307RTC.html#a6fec8ff71f33cc1a129eb0bd009600b0" title="Returns true if the realtime clock has updated since the last call to this function.">DS1307RTC::hasUpdates</a>()
<a name="l00119"></a>00119 {
<a name="l00120"></a>00120 <span class="comment">// If not using a 1 Hz pin or there is no RTC chip available,</span>
<a name="l00121"></a>00121 <span class="comment">// then assume that there is an update available.</span>
<a name="l00122"></a>00122 <span class="keywordflow">if</span> (_oneHzPin == 255 || !_isRealTime)
<a name="l00123"></a>00123 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00124"></a>00124
<a name="l00125"></a>00125 <span class="comment">// The DS1307 updates the internal registers on the falling edge of the</span>
<a name="l00126"></a>00126 <span class="comment">// 1 Hz clock. The values should be ready to read on the rising edge.</span>
<a name="l00127"></a>00127 <span class="keywordtype">bool</span> value = digitalRead(_oneHzPin);
<a name="l00128"></a>00128 <span class="keywordflow">if</span> (value &amp;&amp; !prevOneHz) {
<a name="l00129"></a>00129 prevOneHz = value;
<a name="l00130"></a>00130 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00131"></a>00131 } <span class="keywordflow">else</span> {
<a name="l00132"></a>00132 prevOneHz = value;
<a name="l00133"></a>00133 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00134"></a>00134 }
<a name="l00135"></a>00135 }
<a name="l00136"></a>00136
<a name="l00137"></a>00137 <span class="keyword">inline</span> uint8_t fromBCD(uint8_t value)
<a name="l00138"></a>00138 {
<a name="l00139"></a>00139 <span class="keywordflow">return</span> (value &gt;&gt; 4) * 10 + (value &amp; 0x0F);
<a name="l00140"></a>00140 }
<a name="l00141"></a>00141
<a name="l00142"></a>00142 <span class="keyword">inline</span> uint8_t fromHourBCD(uint8_t value)
<a name="l00143"></a>00143 {
<a name="l00144"></a>00144 <span class="keywordflow">if</span> ((value &amp; 0x40) != 0) {
<a name="l00145"></a>00145 <span class="comment">// 12-hour mode.</span>
<a name="l00146"></a>00146 uint8_t result = ((value &gt;&gt; 4) &amp; 0x01) * 10 + (value &amp; 0x0F);
<a name="l00147"></a>00147 <span class="keywordflow">if</span> ((value &amp; 0x20) != 0)
<a name="l00148"></a>00148 <span class="keywordflow">return</span> (result == 12) ? 12 : (result + 12); <span class="comment">// PM</span>
<a name="l00149"></a>00149 <span class="keywordflow">else</span>
<a name="l00150"></a>00150 <span class="keywordflow">return</span> (result == 12) ? 0 : result; <span class="comment">// AM</span>
<a name="l00151"></a>00151 } <span class="keywordflow">else</span> {
<a name="l00152"></a>00152 <span class="comment">// 24-hour mode.</span>
<a name="l00153"></a>00153 <span class="keywordflow">return</span> fromBCD(value);
<a name="l00154"></a>00154 }
<a name="l00155"></a>00155 }
<a name="l00156"></a>00156
<a name="l00157"></a><a class="code" href="classDS1307RTC.html#acd9800d6df2244b8e4e790480a1d62a6">00157</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#acd9800d6df2244b8e4e790480a1d62a6" title="Reads the current time from the realtime clock into value.">DS1307RTC::readTime</a>(<a class="code" href="structRTCTime.html" title="Stores time information from a realtime clock chip.">RTCTime</a> *value)
<a name="l00158"></a>00158 {
<a name="l00159"></a>00159 <span class="keywordflow">if</span> (_isRealTime) {
<a name="l00160"></a>00160 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00161"></a>00161 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_SECOND);
<a name="l00162"></a>00162 <span class="keywordflow">if</span> (_bus-&gt;<a class="code" href="classI2CMaster.html#a4e5f1a1a4c2242699be5a35fc4872fde" title="Starts a read operation for count bytes by sending the start condition and the I2C control byte...">startRead</a>(DS1307_I2C_ADDRESS, 3)) {
<a name="l00163"></a>00163 value-&gt;<a class="code" href="structRTCTime.html#a87b7c02e535d808dcba04c77e34abb91" title="Second within the minute (0-59)">second</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>() &amp; 0x7F);
<a name="l00164"></a>00164 value-&gt;<a class="code" href="structRTCTime.html#acf2161ca037080dc4b767e636ad8db84" title="Minute within the hour (0-59)">minute</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>());
<a name="l00165"></a>00165 value-&gt;<a class="code" href="structRTCTime.html#a98ba717092ef856dd2b773ba02fcb1a4" title="Hour of the day (0-23)">hour</a> = fromHourBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>());
<a name="l00166"></a>00166 } <span class="keywordflow">else</span> {
<a name="l00167"></a>00167 <span class="comment">// RTC chip is not responding.</span>
<a name="l00168"></a>00168 value-&gt;<a class="code" href="structRTCTime.html#a87b7c02e535d808dcba04c77e34abb91" title="Second within the minute (0-59)">second</a> = 0;
<a name="l00169"></a>00169 value-&gt;<a class="code" href="structRTCTime.html#acf2161ca037080dc4b767e636ad8db84" title="Minute within the hour (0-59)">minute</a> = 0;
<a name="l00170"></a>00170 value-&gt;<a class="code" href="structRTCTime.html#a98ba717092ef856dd2b773ba02fcb1a4" title="Hour of the day (0-23)">hour</a> = 0;
<a name="l00171"></a>00171 }
<a name="l00172"></a>00172 } <span class="keywordflow">else</span> {
<a name="l00173"></a>00173 <a class="code" href="classDS1307RTC.html#acd9800d6df2244b8e4e790480a1d62a6" title="Reads the current time from the realtime clock into value.">RTC::readTime</a>(value);
<a name="l00174"></a>00174 }
<a name="l00175"></a>00175 }
<a name="l00176"></a>00176
<a name="l00177"></a><a class="code" href="classDS1307RTC.html#a6fb56d1690bc30e7995961a5b5e5fdc4">00177</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#a6fb56d1690bc30e7995961a5b5e5fdc4" title="Reads the current date from the realtime clock into value.">DS1307RTC::readDate</a>(<a class="code" href="structRTCDate.html" title="Stores date information from a realtime clock chip.">RTCDate</a> *value)
<a name="l00178"></a>00178 {
<a name="l00179"></a>00179 <span class="keywordflow">if</span> (!_isRealTime) {
<a name="l00180"></a>00180 <a class="code" href="classDS1307RTC.html#a6fb56d1690bc30e7995961a5b5e5fdc4" title="Reads the current date from the realtime clock into value.">RTC::readDate</a>(value);
<a name="l00181"></a>00181 <span class="keywordflow">return</span>;
<a name="l00182"></a>00182 }
<a name="l00183"></a>00183 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00184"></a>00184 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_DATE);
<a name="l00185"></a>00185 <span class="keywordflow">if</span> (_bus-&gt;<a class="code" href="classI2CMaster.html#a4e5f1a1a4c2242699be5a35fc4872fde" title="Starts a read operation for count bytes by sending the start condition and the I2C control byte...">startRead</a>(DS1307_I2C_ADDRESS, 3)) {
<a name="l00186"></a>00186 value-&gt;<a class="code" href="structRTCDate.html#a2d68ff3fb90240df522b41222362704c" title="Day of the month (1-31)">day</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>());
<a name="l00187"></a>00187 value-&gt;<a class="code" href="structRTCDate.html#a6e6196059b36186041a5312400ea9202" title="Month of the year (1-12)">month</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>());
<a name="l00188"></a>00188 value-&gt;<a class="code" href="structRTCDate.html#a7d31822daff3c3fc947386abd897732f" title="Year (4-digit)">year</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>()) + 2000;
<a name="l00189"></a>00189 } <span class="keywordflow">else</span> {
<a name="l00190"></a>00190 <span class="comment">// RTC chip is not responding.</span>
<a name="l00191"></a>00191 value-&gt;<a class="code" href="structRTCDate.html#a2d68ff3fb90240df522b41222362704c" title="Day of the month (1-31)">day</a> = 1;
<a name="l00192"></a>00192 value-&gt;<a class="code" href="structRTCDate.html#a6e6196059b36186041a5312400ea9202" title="Month of the year (1-12)">month</a> = 1;
<a name="l00193"></a>00193 value-&gt;<a class="code" href="structRTCDate.html#a7d31822daff3c3fc947386abd897732f" title="Year (4-digit)">year</a> = 2000;
<a name="l00194"></a>00194 }
<a name="l00195"></a>00195 }
<a name="l00196"></a>00196
<a name="l00197"></a>00197 <span class="keyword">inline</span> uint8_t toBCD(uint8_t value)
<a name="l00198"></a>00198 {
<a name="l00199"></a>00199 <span class="keywordflow">return</span> ((value / 10) &lt;&lt; 4) + (value % 10);
<a name="l00200"></a>00200 }
<a name="l00201"></a>00201
<a name="l00202"></a><a class="code" href="classDS1307RTC.html#a0a5d0d86a0345420ebb289ea724b19e8">00202</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#a0a5d0d86a0345420ebb289ea724b19e8" title="Updates the time in the realtime clock to match value.">DS1307RTC::writeTime</a>(<span class="keyword">const</span> <a class="code" href="structRTCTime.html" title="Stores time information from a realtime clock chip.">RTCTime</a> *value)
<a name="l00203"></a>00203 {
<a name="l00204"></a>00204 <span class="keywordflow">if</span> (_isRealTime) {
<a name="l00205"></a>00205 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00206"></a>00206 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_SECOND);
<a name="l00207"></a>00207 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCTime.html#a87b7c02e535d808dcba04c77e34abb91" title="Second within the minute (0-59)">second</a>));
<a name="l00208"></a>00208 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCTime.html#acf2161ca037080dc4b767e636ad8db84" title="Minute within the hour (0-59)">minute</a>));
<a name="l00209"></a>00209 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCTime.html#a98ba717092ef856dd2b773ba02fcb1a4" title="Hour of the day (0-23)">hour</a>)); <span class="comment">// Changes mode to 24-hour clock.</span>
<a name="l00210"></a>00210 _bus-&gt;<a class="code" href="classI2CMaster.html#ab29f63551ddeb032a91505d1c0b8ac41" title="Ends the current write operation.">endWrite</a>();
<a name="l00211"></a>00211 } <span class="keywordflow">else</span> {
<a name="l00212"></a>00212 <a class="code" href="classDS1307RTC.html#a0a5d0d86a0345420ebb289ea724b19e8" title="Updates the time in the realtime clock to match value.">RTC::writeTime</a>(value);
<a name="l00213"></a>00213 }
<a name="l00214"></a>00214 }
<a name="l00215"></a>00215
<a name="l00216"></a><a class="code" href="classDS1307RTC.html#a4f7346be33612cf9ecd96080eb046230">00216</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#a4f7346be33612cf9ecd96080eb046230" title="Updates the date in the realtime clock to match value.">DS1307RTC::writeDate</a>(<span class="keyword">const</span> <a class="code" href="structRTCDate.html" title="Stores date information from a realtime clock chip.">RTCDate</a> *value)
<a name="l00217"></a>00217 {
<a name="l00218"></a>00218 <span class="keywordflow">if</span> (_isRealTime) {
<a name="l00219"></a>00219 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00220"></a>00220 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_DATE);
<a name="l00221"></a>00221 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCDate.html#a2d68ff3fb90240df522b41222362704c" title="Day of the month (1-31)">day</a>));
<a name="l00222"></a>00222 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCDate.html#a6e6196059b36186041a5312400ea9202" title="Month of the year (1-12)">month</a>));
<a name="l00223"></a>00223 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCDate.html#a7d31822daff3c3fc947386abd897732f" title="Year (4-digit)">year</a> % 100));
<a name="l00224"></a>00224 _bus-&gt;<a class="code" href="classI2CMaster.html#ab29f63551ddeb032a91505d1c0b8ac41" title="Ends the current write operation.">endWrite</a>();
<a name="l00225"></a>00225 } <span class="keywordflow">else</span> {
<a name="l00226"></a>00226 <a class="code" href="classDS1307RTC.html#a4f7346be33612cf9ecd96080eb046230" title="Updates the date in the realtime clock to match value.">RTC::writeDate</a>(value);
<a name="l00227"></a>00227 }
<a name="l00228"></a>00228 }
<a name="l00229"></a>00229
<a name="l00230"></a><a class="code" href="classDS1307RTC.html#aab608eb1630520ee122306b721fdc47a">00230</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#aab608eb1630520ee122306b721fdc47a" title="Reads the details of the alarm with index alarmNum into value.">DS1307RTC::readAlarm</a>(uint8_t alarmNum, <a class="code" href="structRTCAlarm.html" title="Stores alarm information from a realtime clock chip.">RTCAlarm</a> *value)
<a name="l00231"></a>00231 {
<a name="l00232"></a>00232 <span class="keywordflow">if</span> (_isRealTime) {
<a name="l00233"></a>00233 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00234"></a>00234 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_ALARMS + alarmNum * DS1307_ALARM_SIZE);
<a name="l00235"></a>00235 <span class="keywordflow">if</span> (_bus-&gt;<a class="code" href="classI2CMaster.html#a4e5f1a1a4c2242699be5a35fc4872fde" title="Starts a read operation for count bytes by sending the start condition and the I2C control byte...">startRead</a>(DS1307_I2C_ADDRESS, 3)) {
<a name="l00236"></a>00236 value-&gt;<a class="code" href="structRTCAlarm.html#a75bdc42acd3ab3ad495680c6b6a34692" title="Hour of the day for the alarm (0-23).">hour</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>());
<a name="l00237"></a>00237 value-&gt;<a class="code" href="structRTCAlarm.html#ad9a75ceb4b4b3474baa66dd5466e62fe" title="Minute of the hour for the alarm (0-59).">minute</a> = fromBCD(_bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>());
<a name="l00238"></a>00238 value-&gt;<a class="code" href="structRTCAlarm.html#a0f2ef7363cb60a26642d5295b77ca19e" title="Additional flags for the alarm.">flags</a> = _bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>();
<a name="l00239"></a>00239 } <span class="keywordflow">else</span> {
<a name="l00240"></a>00240 <span class="comment">// RTC chip is not responding.</span>
<a name="l00241"></a>00241 value-&gt;<a class="code" href="structRTCAlarm.html#a75bdc42acd3ab3ad495680c6b6a34692" title="Hour of the day for the alarm (0-23).">hour</a> = 0;
<a name="l00242"></a>00242 value-&gt;<a class="code" href="structRTCAlarm.html#ad9a75ceb4b4b3474baa66dd5466e62fe" title="Minute of the hour for the alarm (0-59).">minute</a> = 0;
<a name="l00243"></a>00243 value-&gt;<a class="code" href="structRTCAlarm.html#a0f2ef7363cb60a26642d5295b77ca19e" title="Additional flags for the alarm.">flags</a> = 0;
<a name="l00244"></a>00244 }
<a name="l00245"></a>00245 } <span class="keywordflow">else</span> {
<a name="l00246"></a>00246 <a class="code" href="classDS1307RTC.html#aab608eb1630520ee122306b721fdc47a" title="Reads the details of the alarm with index alarmNum into value.">RTC::readAlarm</a>(alarmNum, value);
<a name="l00247"></a>00247 }
<a name="l00248"></a>00248 }
<a name="l00249"></a>00249
<a name="l00250"></a><a class="code" href="classDS1307RTC.html#a7354aed91d7c94d0d7b2144b1bf32c75">00250</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#a7354aed91d7c94d0d7b2144b1bf32c75" title="Updates the details of the alarm with index alarmNum from value.">DS1307RTC::writeAlarm</a>(uint8_t alarmNum, <span class="keyword">const</span> <a class="code" href="structRTCAlarm.html" title="Stores alarm information from a realtime clock chip.">RTCAlarm</a> *value)
<a name="l00251"></a>00251 {
<a name="l00252"></a>00252 <span class="keywordflow">if</span> (_isRealTime) {
<a name="l00253"></a>00253 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00254"></a>00254 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_ALARMS + alarmNum * DS1307_ALARM_SIZE);
<a name="l00255"></a>00255 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCAlarm.html#a75bdc42acd3ab3ad495680c6b6a34692" title="Hour of the day for the alarm (0-23).">hour</a>));
<a name="l00256"></a>00256 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(toBCD(value-&gt;<a class="code" href="structRTCAlarm.html#ad9a75ceb4b4b3474baa66dd5466e62fe" title="Minute of the hour for the alarm (0-59).">minute</a>));
<a name="l00257"></a>00257 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(value-&gt;<a class="code" href="structRTCAlarm.html#a0f2ef7363cb60a26642d5295b77ca19e" title="Additional flags for the alarm.">flags</a>);
<a name="l00258"></a>00258 _bus-&gt;<a class="code" href="classI2CMaster.html#ab29f63551ddeb032a91505d1c0b8ac41" title="Ends the current write operation.">endWrite</a>();
<a name="l00259"></a>00259 } <span class="keywordflow">else</span> {
<a name="l00260"></a>00260 <a class="code" href="classDS1307RTC.html#a7354aed91d7c94d0d7b2144b1bf32c75" title="Updates the details of the alarm with index alarmNum from value.">RTC::writeAlarm</a>(alarmNum, value);
<a name="l00261"></a>00261 }
<a name="l00262"></a>00262 }
<a name="l00263"></a>00263
<a name="l00264"></a><a class="code" href="classDS1307RTC.html#a93c25269a9b78ab3331354db26672248">00264</a> <span class="keywordtype">int</span> <a class="code" href="classDS1307RTC.html#a93c25269a9b78ab3331354db26672248" title="Returns the number of bytes of non-volatile memory that can be used for storage of arbitrary settings...">DS1307RTC::byteCount</a>()<span class="keyword"> const</span>
<a name="l00265"></a>00265 <span class="keyword"></span>{
<a name="l00266"></a>00266 <span class="keywordflow">return</span> DS1307_ALARMS - DS1307_NVRAM;
<a name="l00267"></a>00267 }
<a name="l00268"></a>00268
<a name="l00269"></a><a class="code" href="classDS1307RTC.html#a7364609a201022688778ab116c3e4f4a">00269</a> uint8_t <a class="code" href="classDS1307RTC.html#a7364609a201022688778ab116c3e4f4a" title="Reads the byte at offset within the realtime clock&#39;s non-volatile memory.">DS1307RTC::readByte</a>(uint8_t offset)
<a name="l00270"></a>00270 {
<a name="l00271"></a>00271 <span class="keywordflow">if</span> (_isRealTime)
<a name="l00272"></a>00272 <span class="keywordflow">return</span> readRegister(DS1307_NVRAM + offset);
<a name="l00273"></a>00273 <span class="keywordflow">else</span>
<a name="l00274"></a>00274 <span class="keywordflow">return</span> <a class="code" href="classDS1307RTC.html#a7364609a201022688778ab116c3e4f4a" title="Reads the byte at offset within the realtime clock&#39;s non-volatile memory.">RTC::readByte</a>(offset);
<a name="l00275"></a>00275 }
<a name="l00276"></a>00276
<a name="l00277"></a><a class="code" href="classDS1307RTC.html#a6ef435fd4aa4adf8eefdf8b1741f5ba6">00277</a> <span class="keywordtype">void</span> <a class="code" href="classDS1307RTC.html#a6ef435fd4aa4adf8eefdf8b1741f5ba6" title="Writes value to offset within the realtime clock&#39;s non-volatile memory.">DS1307RTC::writeByte</a>(uint8_t offset, uint8_t value)
<a name="l00278"></a>00278 {
<a name="l00279"></a>00279 <span class="keywordflow">if</span> (_isRealTime)
<a name="l00280"></a>00280 writeRegister(DS1307_NVRAM + offset, value);
<a name="l00281"></a>00281 <span class="keywordflow">else</span>
<a name="l00282"></a>00282 <a class="code" href="classDS1307RTC.html#a6ef435fd4aa4adf8eefdf8b1741f5ba6" title="Writes value to offset within the realtime clock&#39;s non-volatile memory.">RTC::writeByte</a>(offset, value);
<a name="l00283"></a>00283 }
<a name="l00284"></a>00284
<a name="l00285"></a>00285 <span class="keywordtype">void</span> DS1307RTC::initAlarms()
<a name="l00286"></a>00286 {
<a name="l00287"></a>00287 uint8_t value = readRegister(DS1307_ALARM_MAGIC);
<a name="l00288"></a>00288 <span class="keywordflow">if</span> (value != (0xB0 + <a class="code" href="classRTC.html#aee5ae8f600ee5296e65635c0d836fca3" title="Number of alarms that are supported by RTC::readAlarm() and RTC::writeAlarm().">ALARM_COUNT</a>)) {
<a name="l00289"></a>00289 <span class="comment">// This is the first time we have used this clock chip,</span>
<a name="l00290"></a>00290 <span class="comment">// so initialize all alarms to their default state.</span>
<a name="l00291"></a>00291 <a class="code" href="structRTCAlarm.html" title="Stores alarm information from a realtime clock chip.">RTCAlarm</a> alarm;
<a name="l00292"></a>00292 alarm.<a class="code" href="structRTCAlarm.html#a75bdc42acd3ab3ad495680c6b6a34692" title="Hour of the day for the alarm (0-23).">hour</a> = 6; <span class="comment">// Default to 6am for alarms.</span>
<a name="l00293"></a>00293 alarm.<a class="code" href="structRTCAlarm.html#ad9a75ceb4b4b3474baa66dd5466e62fe" title="Minute of the hour for the alarm (0-59).">minute</a> = 0;
<a name="l00294"></a>00294 alarm.<a class="code" href="structRTCAlarm.html#a0f2ef7363cb60a26642d5295b77ca19e" title="Additional flags for the alarm.">flags</a> = 0;
<a name="l00295"></a>00295 <span class="keywordflow">for</span> (uint8_t index = 0; index &lt; <a class="code" href="classRTC.html#aee5ae8f600ee5296e65635c0d836fca3" title="Number of alarms that are supported by RTC::readAlarm() and RTC::writeAlarm().">ALARM_COUNT</a>; ++index)
<a name="l00296"></a>00296 <a class="code" href="classDS1307RTC.html#a7354aed91d7c94d0d7b2144b1bf32c75" title="Updates the details of the alarm with index alarmNum from value.">writeAlarm</a>(index, &amp;alarm);
<a name="l00297"></a>00297 writeRegister(DS1307_ALARM_MAGIC, 0xB0 + ALARM_COUNT);
<a name="l00298"></a>00298
<a name="l00299"></a>00299 <span class="comment">// Also clear the rest of NVRAM so that it is in a known state.</span>
<a name="l00300"></a>00300 <span class="comment">// Otherwise we&#39;ll have whatever garbage was present at power-on.</span>
<a name="l00301"></a>00301 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00302"></a>00302 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(DS1307_NVRAM);
<a name="l00303"></a>00303 <span class="keywordflow">for</span> (uint8_t index = DS1307_NVRAM; index &lt; DS1307_ALARMS; ++index)
<a name="l00304"></a>00304 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(0);
<a name="l00305"></a>00305 _bus-&gt;<a class="code" href="classI2CMaster.html#ab29f63551ddeb032a91505d1c0b8ac41" title="Ends the current write operation.">endWrite</a>();
<a name="l00306"></a>00306 }
<a name="l00307"></a>00307 }
<a name="l00308"></a>00308
<a name="l00309"></a>00309 uint8_t DS1307RTC::readRegister(uint8_t reg)
<a name="l00310"></a>00310 {
<a name="l00311"></a>00311 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00312"></a>00312 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(reg);
<a name="l00313"></a>00313 <span class="keywordflow">if</span> (!_bus-&gt;<a class="code" href="classI2CMaster.html#a4e5f1a1a4c2242699be5a35fc4872fde" title="Starts a read operation for count bytes by sending the start condition and the I2C control byte...">startRead</a>(DS1307_I2C_ADDRESS, 1))
<a name="l00314"></a>00314 <span class="keywordflow">return</span> 0; <span class="comment">// RTC chip is not responding.</span>
<a name="l00315"></a>00315 <span class="keywordflow">return</span> _bus-&gt;<a class="code" href="classI2CMaster.html#a49eeebb57f6bc06de39973fe836369cd" title="Reads a single byte from the I2C bus.">read</a>();
<a name="l00316"></a>00316 }
<a name="l00317"></a>00317
<a name="l00318"></a>00318 <span class="keywordtype">bool</span> DS1307RTC::writeRegister(uint8_t reg, uint8_t value)
<a name="l00319"></a>00319 {
<a name="l00320"></a>00320 _bus-&gt;<a class="code" href="classI2CMaster.html#a01960fc821cb25e4c88c26d2c6107e35" title="Starts a write operation by sending a start condition and the I2C control byte.">startWrite</a>(DS1307_I2C_ADDRESS);
<a name="l00321"></a>00321 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(reg);
<a name="l00322"></a>00322 _bus-&gt;<a class="code" href="classI2CMaster.html#a0bf6b84cb1e2b3a37a4a0260d0b6f960" title="Writes a single byte value on the I2C bus.">write</a>(value);
<a name="l00323"></a>00323 <span class="keywordflow">return</span> _bus-&gt;<a class="code" href="classI2CMaster.html#ab29f63551ddeb032a91505d1c0b8ac41" title="Ends the current write operation.">endWrite</a>();
<a name="l00324"></a>00324 }
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Mon Jun 18 2012 11:02:51 for ArduinoLibs by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>