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

📄 uartsw_8c-source.html

📁 ATMEL的AVR单片机库文件
💻 HTML
📖 第 1 页 / 共 2 页
字号:
00178             UartswTxData = UartswTxData&gt;&gt;1;00179         }00180         <span class="keywordflow">else</span>00181         {00182             <span class="comment">// transmit stop bit</span>00183             cbi(<a class="code" href="uartsw2conf_8h.html#a1">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a3">UARTSW_TX_PIN</a>);00184         }00185         <span class="comment">// schedule the next bit</span>00186         outw(OCR1A, inw(OCR1A) + UartswBaudRateDiv);00187         <span class="comment">// count down</span>00188         UartswTxBitNum--;00189     }00190     <span class="keywordflow">else</span>00191     {00192         <span class="comment">// transmission is done</span>00193         <span class="comment">// clear busy flag</span>00194         UartswTxBusy = FALSE;00195     }00196 }00197 <a name="l00198"></a><a class="code" href="uartsw2_8h.html#a8">00198</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>(<span class="keywordtype">void</span>)00199 {00200     <span class="comment">// this function runs on either:</span>00201     <span class="comment">// - a rising edge interrupt</span>00202     <span class="comment">// - OC1B</span>00203     <span class="keywordflow">if</span>(!UartswRxBusy)00204     {00205         <span class="comment">// this is a start bit</span>00206         <span class="comment">// disable ICP interrupt</span>00207         cbi(TIMSK, TICIE1);00208         <span class="comment">// schedule data bit sampling 1.5 bit periods from now</span>00209         outw(OCR1B, inw(TCNT1) + UartswBaudRateDiv + UartswBaudRateDiv/2);00210         <span class="comment">// clear OC1B interrupt flag</span>00211         sbi(TIFR, OCF1B);00212         <span class="comment">// enable OC1B interrupt</span>00213         sbi(TIMSK, OCIE1B);00214         <span class="comment">// set start bit flag</span>00215         UartswRxBusy = TRUE;00216         <span class="comment">// reset bit counter</span>00217         UartswRxBitNum = 0;00218         <span class="comment">// reset data</span>00219         UartswRxData = 0;00220     }00221     <span class="keywordflow">else</span>00222     {00223         <span class="comment">// start bit has already been received</span>00224         <span class="comment">// we're in the data bits</span>00225         00226         <span class="comment">// shift data byte to make room for new bit</span>00227         UartswRxData = UartswRxData&gt;&gt;1;00228 00229         <span class="comment">// sample the data line</span>00230         <span class="keywordflow">if</span>( !(inb(<a class="code" href="uartsw2conf_8h.html#a6">UARTSW_RX_PORTIN</a>) &amp; (1&lt;&lt;<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PIN</a>)) )00231         {00232             <span class="comment">// serial line is low</span>00233             <span class="comment">// record '1' bit (data inverted)</span>00234             UartswRxData |= 0x80;00235         }00236 00237         <span class="comment">// increment bit counter</span>00238         UartswRxBitNum++;00239         <span class="comment">// schedule next bit sample</span>00240         outw(OCR1B, inw(OCR1B) + UartswBaudRateDiv);00241 00242         <span class="comment">// check if we have a full byte</span>00243         <span class="keywordflow">if</span>(UartswRxBitNum &gt;= 8)00244         {00245             <span class="comment">// save data in receive buffer</span>00246             <a class="code" href="buffer_8h.html#a5">bufferAddToEnd</a>(&amp;uartswRxBuffer, UartswRxData);00247             <span class="comment">// disable OC1B interrupt</span>00248             cbi(TIMSK, OCIE1B);00249             <span class="comment">// clear ICP interrupt flag</span>00250             sbi(TIFR, ICF1);00251             <span class="comment">// enable ICP interrupt</span>00252             sbi(TIMSK, TICIE1);00253             <span class="comment">// clear start bit flag</span>00254             UartswRxBusy = FALSE;00255         }00256     }00257 }00258 00259 <span class="comment">/*</span>00260 <span class="comment">void uartswRxBitService(void)</span>00261 <span class="comment">{</span>00262 <span class="comment">    u16 thisBitTime;</span>00263 <span class="comment">    u08 bitperiods;</span>00264 <span class="comment">    u08 i;</span>00265 <span class="comment"></span>00266 <span class="comment">    // bit transition was detected</span>00267 <span class="comment">    // record bit's edge time</span>00268 <span class="comment">    thisBitTime = inw(ICR1);</span>00269 <span class="comment"></span>00270 <span class="comment">    cbi(PORTB, 0);</span>00271 <span class="comment"></span>00272 <span class="comment">    if(!UartswRxStartBit)</span>00273 <span class="comment">    {</span>00274 <span class="comment">        // this is a start bit</span>00275 <span class="comment">        // switch to falling-edge trigger</span>00276 <span class="comment">        cbi(TCCR1B, ICES1);</span>00277 <span class="comment">        // record bit time</span>00278 <span class="comment">        UartswRxBitTime = thisBitTime;</span>00279 <span class="comment">        // set start bit flag</span>00280 <span class="comment">        UartswRxStartBit = TRUE;</span>00281 <span class="comment">        // reset bit counter</span>00282 <span class="comment">        UartswRxBitNum = 0;</span>00283 <span class="comment">        // reset data</span>00284 <span class="comment">        UartswRxData = 0;</span>00285 <span class="comment">    }</span>00286 <span class="comment">    else</span>00287 <span class="comment">    {</span>00288 <span class="comment">        // start bit has already been received</span>00289 <span class="comment">        // we're in the data bits</span>00290 <span class="comment">        </span>00291 <span class="comment">        // how many bit periods since last edge?</span>00292 <span class="comment">        bitperiods = (thisBitTime - UartswRxBitTime + UartswBaudRateDiv/2)/UartswBaudRateDiv;</span>00293 <span class="comment">        // set last edge time</span>00294 <span class="comment">        UartswRxBitTime = thisBitTime;</span>00295 <span class="comment"></span>00296 <span class="comment">        if(bitperiods &gt; 10)</span>00297 <span class="comment">        {</span>00298 <span class="comment">            // switch to trigger on rising edge</span>00299 <span class="comment">            sbi(TCCR1B, ICES1);</span>00300 <span class="comment">            // clear start bit flag</span>00301 <span class="comment">            UartswRxStartBit = FALSE;</span>00302 <span class="comment">        }</span>00303 <span class="comment">        else</span>00304 <span class="comment">        {</span>00305 <span class="comment"></span>00306 <span class="comment"></span>00307 <span class="comment">        if( inb(TCCR1B) &amp; (1&lt;&lt;ICES1) )</span>00308 <span class="comment">        {</span>00309 <span class="comment">            // just triggered on a rising edge</span>00310 <span class="comment">            // previous bits were zero</span>00311 <span class="comment">            // shift in the data (data bits are inverted)</span>00312 <span class="comment">            for(i=0; i&lt;bitperiods; i++)</span>00313 <span class="comment">            {</span>00314 <span class="comment">                UartswRxData = UartswRxData&lt;&lt;1;</span>00315 <span class="comment">                UartswRxData |= 0x01;</span>00316 <span class="comment">            }</span>00317 <span class="comment">            // switch to trigger on falling edge</span>00318 <span class="comment">            cbi(TCCR1B, ICES1);</span>00319 <span class="comment">        }</span>00320 <span class="comment">        else</span>00321 <span class="comment">        {</span>00322 <span class="comment">            // just triggered on a falling edge</span>00323 <span class="comment">            // previous bits were one</span>00324 <span class="comment">            // shift in the data (data bits are inverted)</span>00325 <span class="comment">            for(i=0; i&lt;bitperiods; i++)</span>00326 <span class="comment">            {</span>00327 <span class="comment">                UartswRxData = UartswRxData&lt;&lt;1;</span>00328 <span class="comment">            }</span>00329 <span class="comment">            // switch to trigger on rising edge</span>00330 <span class="comment">            sbi(TCCR1B, ICES1);</span>00331 <span class="comment">        }</span>00332 <span class="comment">        </span>00333 <span class="comment">        // increment bit counter</span>00334 <span class="comment">        UartswRxBitNum += bitperiods;</span>00335 <span class="comment">        </span>00336 <span class="comment">        // check if we have a full byte + start bit</span>00337 <span class="comment">        if(bitperiods &gt; 8)</span>00338 <span class="comment">        {</span>00339 <span class="comment">            // save data in receive buffer</span>00340 <span class="comment">            bufferAddToEnd(&amp;uartswRxBuffer, UartswRxData);</span>00341 <span class="comment">            // switch to trigger on rising edge</span>00342 <span class="comment">            sbi(TCCR1B, ICES1);</span>00343 <span class="comment">            // clear start bit flag</span>00344 <span class="comment">            UartswRxStartBit = FALSE;</span>00345 <span class="comment">        }</span>00346 <span class="comment">        }</span>00347 <span class="comment">    }</span>00348 <span class="comment"></span>00349 <span class="comment">    // turn off debug LEDs</span>00350 <span class="comment">    delay(10);</span>00351 <span class="comment">    sbi(PORTB, 0);</span>00352 <span class="comment">    sbi(PORTB, 1);</span>00353 <span class="comment">}</span>00354 <span class="comment">*/</span></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 + -