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

📄 i2c_8c-source.html

📁 单片机的软件
💻 HTML
📖 第 1 页 / 共 3 页
字号:
00199     <span class="comment">// wait for data</span>00200     <span class="keywordflow">while</span>(I2cState);00201     <span class="comment">// return data</span>00202     <span class="keywordflow">for</span>(i=0; i&lt;length; i++)00203         *data++ = I2cReceiveData[i];00204 }00205 <a name="l00206"></a><a class="code" href="i2c_8h.html#a52">00206</a> u08 <a class="code" href="i2c_8c.html#a24">i2cMasterSendNI</a>(u08 deviceAddr, u08 length, u08* data)00207 {00208     u08 retval = I2C_OK;00209 00210     <span class="comment">// disable TWI interrupt</span>00211     cbi(TWCR, TWIE);00212 00213     <span class="comment">// send start condition</span>00214     <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00215     <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00216 00217     <span class="comment">// send device address with write</span>00218     <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>( deviceAddr &amp; 0xFE );00219     <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00220 00221     <span class="comment">// check if device is present and live</span>00222     <span class="keywordflow">if</span>( inb(TWSR) == TW_MT_SLA_ACK)00223     {00224         <span class="comment">// send data</span>00225         <span class="keywordflow">while</span>(length)00226         {00227             <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>( *data++ );00228             <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00229             length--;00230         }00231     }00232     <span class="keywordflow">else</span>00233     {00234         <span class="comment">// device did not ACK it's address,</span>00235         <span class="comment">// data will not be transferred</span>00236         <span class="comment">// return error</span>00237         retval = I2C_ERROR_NODEV;00238     }00239 00240     <span class="comment">// transmit stop condition</span>00241     <span class="comment">// leave with TWEA on for slave receiving</span>00242     <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00243     <span class="keywordflow">while</span>( !(inb(TWCR) &amp; BV(TWSTO)) );00244 00245     <span class="comment">// enable TWI interrupt</span>00246     sbi(TWCR, TWIE);00247 00248     <span class="keywordflow">return</span> retval;00249 }00250 <a name="l00251"></a><a class="code" href="i2c_8h.html#a53">00251</a> u08 <a class="code" href="i2c_8c.html#a25">i2cMasterReceiveNI</a>(u08 deviceAddr, u08 length, u08 *data)00252 {00253     u08 retval = I2C_OK;00254 00255     <span class="comment">// disable TWI interrupt</span>00256     cbi(TWCR, TWIE);00257 00258     <span class="comment">// send start condition</span>00259     <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00260     <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00261 00262     <span class="comment">// send device address with read</span>00263     <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>( deviceAddr | 0x01 );00264     <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00265 00266     <span class="comment">// check if device is present and live</span>00267     <span class="keywordflow">if</span>( inb(TWSR) == TW_MR_SLA_ACK)00268     {00269         <span class="comment">// accept receive data and ack it</span>00270         <span class="keywordflow">while</span>(length &gt; 1)00271         {00272             <a class="code" href="i2c_8c.html#a19">i2cReceiveByte</a>(TRUE);00273             <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00274             *data++ = <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>();00275             <span class="comment">// decrement length</span>00276             length--;00277         }00278 00279         <span class="comment">// accept receive data and nack it (last-byte signal)</span>00280         <a class="code" href="i2c_8c.html#a19">i2cReceiveByte</a>(FALSE);00281         <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00282         *data++ = <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>();00283     }00284     <span class="keywordflow">else</span>00285     {00286         <span class="comment">// device did not ACK it's address,</span>00287         <span class="comment">// data will not be transferred</span>00288         <span class="comment">// return error</span>00289         retval = I2C_ERROR_NODEV;00290     }00291 00292     <span class="comment">// transmit stop condition</span>00293     <span class="comment">// leave with TWEA on for slave receiving</span>00294     <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00295 00296     <span class="comment">// enable TWI interrupt</span>00297     sbi(TWCR, TWIE);00298 00299     <span class="keywordflow">return</span> retval;00300 }00301 <span class="comment">/*</span>00302 <span class="comment">void i2cMasterTransferNI(u08 deviceAddr, u08 sendlength, u08* senddata, u08 receivelength, u08* receivedata)</span>00303 <span class="comment">{</span>00304 <span class="comment">    // disable TWI interrupt</span>00305 <span class="comment">    cbi(TWCR, TWIE);</span>00306 <span class="comment"></span>00307 <span class="comment">    // send start condition</span>00308 <span class="comment">    i2cSendStart();</span>00309 <span class="comment">    i2cWaitForComplete();</span>00310 <span class="comment"></span>00311 <span class="comment">    // if there's data to be sent, do it</span>00312 <span class="comment">    if(sendlength)</span>00313 <span class="comment">    {</span>00314 <span class="comment">        // send device address with write</span>00315 <span class="comment">        i2cSendByte( deviceAddr &amp; 0xFE );</span>00316 <span class="comment">        i2cWaitForComplete();</span>00317 <span class="comment">        </span>00318 <span class="comment">        // send data</span>00319 <span class="comment">        while(sendlength)</span>00320 <span class="comment">        {</span>00321 <span class="comment">            i2cSendByte( *senddata++ );</span>00322 <span class="comment">            i2cWaitForComplete();</span>00323 <span class="comment">            sendlength--;</span>00324 <span class="comment">        }</span>00325 <span class="comment">    }</span>00326 <span class="comment"></span>00327 <span class="comment">    // if there's data to be received, do it</span>00328 <span class="comment">    if(receivelength)</span>00329 <span class="comment">    {</span>00330 <span class="comment">        // send repeated start condition</span>00331 <span class="comment">        i2cSendStart();</span>00332 <span class="comment">        i2cWaitForComplete();</span>00333 <span class="comment"></span>00334 <span class="comment">        // send device address with read</span>00335 <span class="comment">        i2cSendByte( deviceAddr | 0x01 );</span>00336 <span class="comment">        i2cWaitForComplete();</span>00337 <span class="comment"></span>00338 <span class="comment">        // accept receive data and ack it</span>00339 <span class="comment">        while(receivelength &gt; 1)</span>00340 <span class="comment">        {</span>00341 <span class="comment">            i2cReceiveByte(TRUE);</span>00342 <span class="comment">            i2cWaitForComplete();</span>00343 <span class="comment">            *receivedata++ = i2cGetReceivedByte();</span>00344 <span class="comment">            // decrement length</span>00345 <span class="comment">            receivelength--;</span>00346 <span class="comment">        }</span>00347 <span class="comment"></span>00348 <span class="comment">        // accept receive data and nack it (last-byte signal)</span>00349 <span class="comment">        i2cReceiveByte(TRUE);</span>00350 <span class="comment">        i2cWaitForComplete();</span>00351 <span class="comment">        *receivedata++ = i2cGetReceivedByte();</span>00352 <span class="comment">    }</span>00353 <span class="comment">    </span>00354 <span class="comment">    // transmit stop condition</span>00355 <span class="comment">    // leave with TWEA on for slave receiving</span>00356 <span class="comment">    i2cSendStop();</span>00357 <span class="comment">    while( !(inb(TWCR) &amp; BV(TWSTO)) );</span>00358 <span class="comment"></span>00359 <span class="comment">    // enable TWI interrupt</span>00360 <span class="comment">    sbi(TWCR, TWIE);</span>00361 <span class="comment">}</span>00362 <span class="comment">*/</span>00363 <span class="comment"></span>00364 <span class="comment">//! I2C (TWI) interrupt service routine</span><a name="l00365"></a><a class="code" href="i2c_8c.html#a26">00365</a> <span class="comment"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_2WIRE_SERIAL)00366 {00367     <span class="comment">// read status bits</span>00368     u08 status = inb(TWSR) &amp; TWSR_STATUS_MASK;00369 00370     <span class="keywordflow">switch</span>(status)00371     {00372     <span class="comment">// Master General</span>00373     <span class="keywordflow">case</span> TW_START:                      <span class="comment">// 0x08: Sent start condition</span>00374     <span class="keywordflow">case</span> TW_REP_START:                  <span class="comment">// 0x10: Sent repeated start condition</span>00375 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00376 <span class="preprocessor"></span>        <a class="code" href="group__rprintf.html#ga0">rprintfInit</a>(uart1AddToTxBuffer);00377         rprintf(<span class="stringliteral">"I2C: M-&gt;START\r\n"</span>);00378         <a class="code" href="group__rprintf.html#ga0">rprintfInit</a>(uart1SendByte);00379 <span class="preprocessor">        #endif</span>00380 <span class="preprocessor"></span>        <span class="comment">// send device address</span>00381         <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>(I2cDeviceAddrRW);00382         <span class="keywordflow">break</span>;00383     00384     <span class="comment">// Master Transmitter &amp; Receiver status codes</span>00385     <span class="keywordflow">case</span> TW_MT_SLA_ACK:                 <span class="comment">// 0x18: Slave address acknowledged</span>00386     <span class="keywordflow">case</span> TW_MT_DATA_ACK:                <span class="comment">// 0x28: Data acknowledged</span>00387 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00388 <span class="preprocessor"></span>        <a class="code" href="group__rprintf.html#ga0">rprintfInit</a>(uart1AddToTxBuffer);00389         rprintf(<span class="stringliteral">"I2C: MT-&gt;SLA_ACK or DATA_ACK\r\n"</span>);00390         <a class="code" href="group__rprintf.html#ga0">rprintfInit</a>(uart1SendByte);00391 <span class="preprocessor">        #endif</span>00392 <span class="preprocessor"></span>        <span class="keywordflow">if</span>(I2cSendDataIndex &lt; I2cSendDataLength)00393         {00394             <span class="comment">// send data</span>00395             <a class="code" href="i2c_8c.html#a18">i2cSendByte</a>( I2cSendData[I2cSendDataIndex++] );00396         }00397         <span class="keywordflow">else</span>00398         {00399             <span class="comment">// transmit stop condition, enable SLA ACK</span>00400             <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00401             <span class="comment">// set state</span>00402             I2cState = I2C_IDLE;00403         }

⌨️ 快捷键说明

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