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

📄 i2csw_8c-source.html

📁 ATMEL的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: i2csw.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><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="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>i2csw.c</h1><a href="i2csw_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file i2csw.c \brief Software-driven I2C interface using port pins. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'i2csw.c'</span>00005 <span class="comment">// Title        : Software-driven I2C interface using port pins</span>00006 <span class="comment">// Author       : Pascal Stang</span>00007 <span class="comment">// Created      : 11/22/2000</span>00008 <span class="comment">// Revised      : 5/2/2002</span>00009 <span class="comment">// Version      : 1.1</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">// This code is distributed under the GNU Public License</span>00014 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>00015 <span class="comment">//</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>00019 00020 <span class="preprocessor">#include "<a class="code" href="i2csw_8h.html">i2csw.h</a>"</span>00021 00022 <span class="comment">// Standard I2C bit rates are:</span>00023 <span class="comment">// 100KHz for slow speed</span>00024 <span class="comment">// 400KHz for high speed</span>00025 00026 <span class="comment">//#define QDEL  delay(5)        // i2c quarter-bit delay</span>00027 <span class="comment">//#define HDEL  delay(10)       // i2c half-bit delay</span>00028 00029 <span class="comment">// i2c quarter-bit delay</span>00030 <span class="preprocessor">#define QDEL    asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop");</span>00031 <span class="preprocessor"></span><span class="comment">// i2c half-bit delay</span>00032 <span class="preprocessor">#define HDEL    asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop");</span>00033 <span class="preprocessor"></span>00034 <span class="preprocessor">#define I2C_SDL_LO      cbi( SDAPORT, SDA)</span>00035 <span class="preprocessor"></span><span class="preprocessor">#define I2C_SDL_HI      sbi( SDAPORT, SDA)</span>00036 <span class="preprocessor"></span>00037 <span class="preprocessor">#define I2C_SCL_LO      cbi( SCLPORT, SCL); </span>00038 <span class="preprocessor"></span><span class="preprocessor">#define I2C_SCL_HI      sbi( SCLPORT, SCL); </span>00039 <span class="preprocessor"></span>00040 <span class="preprocessor">#define I2C_SCL_TOGGLE  HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO;</span>00041 <span class="preprocessor"></span><span class="preprocessor">#define I2C_START       I2C_SDL_LO; QDEL; I2C_SCL_LO; </span>00042 <span class="preprocessor"></span><span class="preprocessor">#define I2C_STOP        HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL;</span>00043 <span class="preprocessor"></span>00044 <span class="comment">/*</span>00045 <span class="comment">void i2ct(void)</span>00046 <span class="comment">{</span>00047 <span class="comment">    HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO;</span>00048 <span class="comment">}</span>00049 <span class="comment"></span>00050 <span class="comment">void i2cstart(void)</span>00051 <span class="comment">{</span>00052 <span class="comment">    I2C_SDL_LO; QDEL; I2C_SCL_LO; </span>00053 <span class="comment">}</span>00054 <span class="comment"></span>00055 <span class="comment">void i2cstop(void)</span>00056 <span class="comment">{</span>00057 <span class="comment">    HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL;</span>00058 <span class="comment">}</span>00059 <span class="comment"></span>00060 <span class="comment"></span>00061 <span class="comment">#define I2C_SCL_TOGGLE  i2ct();</span>00062 <span class="comment">#define I2C_START       i2cstart();</span>00063 <span class="comment">#define I2C_STOP        i2cstop();  </span>00064 <span class="comment">*/</span>00065 00066 UINT i2cPutbyte(u08 b)00067 {00068     <span class="keywordtype">int</span> i;00069     00070     <span class="keywordflow">for</span> (i=7;i&gt;=0;i--)00071     {00072         <span class="keywordflow">if</span> ( b &amp; (1&lt;&lt;i) )00073             I2C_SDL_HI;00074         <span class="keywordflow">else</span>00075             I2C_SDL_LO;         <span class="comment">// address bit</span>00076             I2C_SCL_TOGGLE;     <span class="comment">// clock HI, delay, then LO</span>00077     }00078 00079     I2C_SDL_HI;                 <span class="comment">// leave SDL HI</span>00080     <span class="comment">// added    </span>00081     cbi(SDADDR, SDA);           <span class="comment">// change direction to input on SDA line (may not be needed)</span>00082     HDEL;00083     I2C_SCL_HI;                 <span class="comment">// clock back up</span>00084     b = inp(SDAPIN) &amp; (1&lt;&lt;SDA); <span class="comment">// get the ACK bit</span>00085 00086     HDEL;00087     I2C_SCL_LO;                 <span class="comment">// not really ??</span>00088     sbi(SDADDR, SDA);           <span class="comment">// change direction back to output</span>00089     HDEL;00090     <span class="keywordflow">return</span> (b == 0);            <span class="comment">// return ACK value</span>00091 }00092 00093 00094 u08 i2cGetbyte(UINT last)00095 {00096     <span class="keywordtype">int</span> i;00097     u08 c,b = 0;00098         00099     I2C_SDL_HI;                 <span class="comment">// make sure pullups are ativated</span>00100     cbi(SDADDR, SDA);           <span class="comment">// change direction to input on SDA line (may not be needed)</span>00101 00102     <span class="keywordflow">for</span>(i=7;i&gt;=0;i--)00103     {00104         HDEL;00105         I2C_SCL_HI;             <span class="comment">// clock HI</span>00106         c = inp(SDAPIN) &amp; (1&lt;&lt;SDA);  00107         b &lt;&lt;= 1;00108         <span class="keywordflow">if</span>(c) b |= 1;00109         HDEL;00110         I2C_SCL_LO;             <span class="comment">// clock LO</span>00111     }00112 00113     sbi(SDADDR, SDA);           <span class="comment">// change direction to output on SDA line</span>00114   00115     <span class="keywordflow">if</span> (last)00116         I2C_SDL_HI;             <span class="comment">// set NAK</span>00117     <span class="keywordflow">else</span>00118         I2C_SDL_LO;             <span class="comment">// set ACK</span>00119 00120     I2C_SCL_TOGGLE;             <span class="comment">// clock pulse</span>00121     I2C_SDL_HI;                 <span class="comment">// leave with SDL HI</span>00122     <span class="keywordflow">return</span> b;                   <span class="comment">// return received byte</span>00123 }00124 00125 00126 <span class="comment">//************************</span>00127 <span class="comment">//* I2C public functions *</span>00128 <span class="comment">//************************</span>00129 <span class="comment"></span>00130 <span class="comment">//! Initialize I2C communication</span><a name="l00131"></a><a class="code" href="i2csw_8c.html#a11">00131</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2c_8c.html#a10">i2cInit</a>(<span class="keywordtype">void</span>)00132 {00133     sbi( SDADDR, SDA);          <span class="comment">// set SDA as output</span>00134     sbi( SCLDDR, SCL);          <span class="comment">// set SCL as output</span>00135     I2C_SDL_HI;                 <span class="comment">// set I/O state and pull-ups</span>00136     I2C_SCL_HI;                 <span class="comment">// set I/O state and pull-ups</span>00137 }00138 <span class="comment"></span>00139 <span class="comment">//! Send a byte sequence on the I2C bus</span><a name="l00140"></a><a class="code" href="i2csw_8c.html#a12">00140</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2csw_8c.html#a12">i2cSend</a>(u08 device, u08 subAddr, u08 length, u08 *data)00141 {00142     I2C_START;                  <span class="comment">// do start transition</span>00143     i2cPutbyte(device);         <span class="comment">// send DEVICE address</span>00144     i2cPutbyte(subAddr);        <span class="comment">// and the subaddress</span>00145 00146     <span class="comment">// send the data</span>00147     <span class="keywordflow">while</span> (length--)00148         i2cPutbyte(*data++);00149 00150     I2C_SDL_LO;                 <span class="comment">// clear data line and</span>00151     I2C_STOP;                   <span class="comment">// send STOP transition</span>00152 }00153 <span class="comment"></span>00154 <span class="comment">//! Retrieve a byte sequence on the I2C bus</span><a name="l00155"></a><a class="code" href="i2csw_8c.html#a13">00155</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="i2csw_8c.html#a13">i2cReceive</a>(u08 device, u08 subAddr, u08 length, u08 *data)00156 {00157     <span class="keywordtype">int</span> j = length;00158     u08 *p = data;00159 00160     I2C_START;                  <span class="comment">// do start transition</span>00161     i2cPutbyte(device);         <span class="comment">// send DEVICE address</span>00162     i2cPutbyte(subAddr);        <span class="comment">// and the subaddress</span>00163     HDEL;00164     I2C_SCL_HI;                 <span class="comment">// do a repeated START</span>00165     I2C_START;                  <span class="comment">// transition</span>00166 00167     i2cPutbyte(device | READ);  <span class="comment">// resend DEVICE, with READ bit set</span>00168 00169     <span class="comment">// receive data bytes</span>00170     <span class="keywordflow">while</span> (j--)00171         *p++ = i2cGetbyte(j == 0);00172 00173     I2C_SDL_LO;                 <span class="comment">// clear data line and</span>00174     I2C_STOP;                   <span class="comment">// send STOP transition</span>00175 }00176 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Oct 15 03:50:22 2004 for Procyon AVRlib by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>

⌨️ 快捷键说明

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