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

📄 i2c_8c-source.html

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

⌨️ 快捷键说明

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