<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Arduino中国 &#187; DS1307</title>
	<atom:link href="http://blog.flamingoeda.com/tag/ds1307/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flamingoeda.com</link>
	<description>Flamingo EDA</description>
	<lastBuildDate>Sat, 13 Aug 2011 11:41:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>电子积木 实时钟模块 RTC DS1307</title>
		<link>http://blog.flamingoeda.com/2010/07/16/%e7%94%b5%e5%ad%90%e7%a7%af%e6%9c%a8-%e5%ae%9e%e6%97%b6%e9%92%9f%e6%a8%a1%e5%9d%97-rtc-ds1307/</link>
		<comments>http://blog.flamingoeda.com/2010/07/16/%e7%94%b5%e5%ad%90%e7%a7%af%e6%9c%a8-%e5%ae%9e%e6%97%b6%e9%92%9f%e6%a8%a1%e5%9d%97-rtc-ds1307/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 15:45:25 +0000</pubDate>
		<dc:creator>flamingoeda</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[电子积木]]></category>
		<category><![CDATA[DS1307]]></category>
		<category><![CDATA[RTC]]></category>
		<category><![CDATA[实时钟]]></category>

		<guid isPermaLink="false">http://blog.flamingoeda.com/?p=607</guid>
		<description><![CDATA[实时钟通常也被称为实时时钟，它能够向电子电路提供日期和时间信息，包括年、月、日、时、分、秒，被广泛应用在需要进行计时的场合中。许多实时钟电路还提供电池供电的方式，这样在发生掉电时仍能准确计时。通常说来，功能稍多一点的实时钟电路还会提供包括警报、看门狗，以及支持高精度要求的校准寄存器等附加功能。 DS1307是DALLAS公司的一款实时种芯片，采用I2C协议与单片机通讯，而Arduino上正好有这一接口，因此连接起来就非常方便了。DS1307中有一个可编程波形输出口，它可以用来驱动LED小灯，或者作为中断来触发某些事件，不过用它去带一些大功率的东西的时候要注意。我们设计的这一款实时钟模块，将Ds1307的I2C接口和可编程波形输出接口SQW都连接出来了，不过一般情况下我们只会用到I2C接口来实现基本的时钟设置/读取功能。需要注意的是，该模块必须先安装上电池才可以正常工作。电池使用的是纽扣电池（型号CR1220），正极朝上： 在电路连接上我们可以使用Arduino专用传感器扩展板V4，不过要将相应的跳线设置到IIC的位置上： 剩下的工作就是用4芯的I2C/COM连接线将传感器扩展板上的专用接口，与实时钟模块上的IIC（I2C其实就是IIC的缩写）端口连接起来了： 硬件连接的工作完成之后，如何在Arduino里对该模块进行编程呢？上网搜索了一下，发现在Arduino上使用DS1307做为时钟芯片的玩家还真不少，而且还封装好了相应的Arduino库，实验时我使用的是Google Code上的这个DS1307库，你也可以在这里下载到我使用的版本。将下载好的压缩文件解压缩到Arduino 0018的libraries目录下后，重新启动Arduino并用它自带的测试程序进行测试： #include &#60;WProgram.h&#62; #include &#60;Wire.h&#62; #include &#60;DS1307.h&#62; // written by mattt on the Arduino forum and modified by D. Sjunnesson void setup() { Serial.begin(9600); RTC.stop(); RTC.set(DS1307_SEC,1); //set the seconds RTC.set(DS1307_MIN,23); //set the minutes RTC.set(DS1307_HR,12); //set the hours RTC.set(DS1307_DOW,4); //set the day of the week RTC.set(DS1307_DATE,15); //set the date RTC.set(DS1307_MTH,7); [...]]]></description>
			<content:encoded><![CDATA[<p>实时钟通常也被称为实时时钟，它能够向电子电路提供日期和时间信息，包括年、月、日、时、分、秒，被广泛应用在需要进行计时的场合中。许多实时钟电路还提供电池供电的方式，这样在发生掉电时仍能准确计时。通常说来，功能稍多一点的实时钟电路还会提供包括警报、看门狗，以及支持高精度要求的校准寄存器等附加功能。</p>
<p>DS1307是DALLAS公司的一款实时种芯片，采用I2C协议与单片机通讯，而Arduino上正好有这一接口，因此连接起来就非常方便了。DS1307中有一个可编程波形输出口，它可以用来驱动LED小灯，或者作为中断来触发某些事件，不过用它去带一些大功率的东西的时候要注意。我们设计的这一款实时钟模块，将Ds1307的I2C接口和可编程波形输出接口SQW都连接出来了，不过一般情况下我们只会用到I2C接口来实现基本的时钟设置/读取功能。需要注意的是，该模块必须先安装上电池才可以正常工作。电池使用的是纽扣电池（型号CR1220），正极朝上：</p>
<p><img class="aligncenter" title="rtc" src="http://image.flamingoeda.com/albums/userpics/febb_rtc_ds1307_1.JPG" alt="" width="460" height="345" /></p>
<p>在电路连接上我们可以使用Arduino专用传感器扩展板V4，不过要将相应的跳线设置到IIC的位置上：</p>
<p><img class="aligncenter" title="rtc" src="http://image.flamingoeda.com/albums/userpics/febb_rtc_ds1307_2.JPG" alt="" width="230" height="173" /></p>
<p>剩下的工作就是用4芯的I2C/COM连接线将传感器扩展板上的专用接口，与实时钟模块上的IIC（I2C其实就是IIC的缩写）端口连接起来了：</p>
<p><img class="aligncenter" title="rtc" src="http://image.flamingoeda.com/albums/userpics/febb_rtc_ds1307_3.JPG" alt="" width="460" height="345" /></p>
<p>硬件连接的工作完成之后，如何在Arduino里对该模块进行编程呢？上网搜索了一下，发现在Arduino上使用DS1307做为时钟芯片的玩家还真不少，而且还封装好了相应的Arduino库，实验时我使用的是<a href="http://code.google.com/p/ds1307/downloads/list">Google Code上的这个DS1307库</a>，你也可以<a href="http://blog.flamingoeda.com/wp-content/uploads/2010/07/DS1307.zip">在这里下载</a>到我使用的版本。将下载好的压缩文件解压缩到Arduino 0018的libraries目录下后，重新启动Arduino并用它自带的测试程序进行测试：</p>
<pre>#include &lt;<span style="color: #cc6600;">WProgram</span>.h&gt;
#include &lt;<span style="color: #cc6600;">Wire</span>.h&gt;
#include &lt;<span style="color: #cc6600;">DS1307</span>.h&gt; <span style="color: #7e7e7e;">// written by  mattt on the Arduino forum and modified by D. Sjunnesson</span>

<span style="color: #cc6600;">void</span> <span style="color: #cc6600;"><strong>setup</strong></span>()
{
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">begin</span>(9600);

  RTC.<span style="color: #cc6600;">stop</span>();
  RTC.set(DS1307_SEC,1);        <span style="color: #7e7e7e;">//set the seconds</span>
  RTC.set(DS1307_MIN,23);     <span style="color: #7e7e7e;">//set the minutes</span>
  RTC.set(DS1307_HR,12);       <span style="color: #7e7e7e;">//set the hours</span>
  RTC.set(DS1307_DOW,4);       <span style="color: #7e7e7e;">//set the day of the week</span>
  RTC.set(DS1307_DATE,15);       <span style="color: #7e7e7e;">//set the date</span>
  RTC.set(DS1307_MTH,7);        <span style="color: #7e7e7e;">//set the month</span>
  RTC.set(DS1307_YR,10);         <span style="color: #7e7e7e;">//set the year</span>
  RTC.start();

}

<span style="color: #cc6600;">void</span> <span style="color: #cc6600;"><strong>loop</strong></span>()
{

  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(RTC.get(DS1307_HR,<span style="color: #cc6600;">true</span>)); <span style="color: #7e7e7e;">//read the hour and also update all the values by pushing in true</span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(<span style="color: #006699;">":"</span>);
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(RTC.get(DS1307_MIN,<span style="color: #cc6600;">false</span>));<span style="color: #7e7e7e;">//read minutes without update (false)</span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(<span style="color: #006699;">":"</span>);
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(RTC.get(DS1307_SEC,<span style="color: #cc6600;">false</span>));<span style="color: #7e7e7e;">//read seconds</span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(<span style="color: #006699;">"      "</span>);                 <span style="color: #7e7e7e;">// some space for a more happy life</span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(RTC.get(DS1307_DATE,<span style="color: #cc6600;">false</span>));<span style="color: #7e7e7e;">//read date</span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(<span style="color: #006699;">"/"</span>);
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(RTC.get(DS1307_MTH,<span style="color: #cc6600;">false</span>));<span style="color: #7e7e7e;">//read month</span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(<span style="color: #006699;">"/"</span>);
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">print</span>(RTC.get(DS1307_YR,<span style="color: #cc6600;">false</span>)); <span style="color: #7e7e7e;">//read year </span>
  <span style="color: #cc6600;"><strong>Serial</strong></span>.<span style="color: #cc6600;">println</span>();

  <span style="color: #cc6600;">delay</span>(1000);
}
</pre>
<p>程序读起来应该不算困难，基本上就是使用RTC.set来对时钟进行设置，然后就可以通过RTC.get来读取相应的时间信息了，至于时钟怎么维护，那就是 DS1307 的工作了:)</p>
<p>将程序下载到Arduino里并运行起来之后，就可以通过串口监控窗口来查看相应的输出结果了：<br />
<a href="http://blog.flamingoeda.com/wp-content/uploads/2010/07/febb_rtc_ds1307_4.jpg"><img class="aligncenter size-medium wp-image-610" title="febb_rtc_ds1307_4" src="http://blog.flamingoeda.com/wp-content/uploads/2010/07/febb_rtc_ds1307_4-260x300.jpg" alt="" width="260" height="300" /></a>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.flamingoeda.com/2010/07/16/%e7%94%b5%e5%ad%90%e7%a7%af%e6%9c%a8-%e5%ae%9e%e6%97%b6%e9%92%9f%e6%a8%a1%e5%9d%97-rtc-ds1307/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

