📄 uartsw2_8c-source.html
字号:
00155 UartswTxBitNum = 9;00156 00157 <span class="comment">// set the start bit</span>00158 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00159 <span class="preprocessor"></span> sbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00160 <span class="preprocessor"> #else</span>00161 <span class="preprocessor"></span> cbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00162 <span class="preprocessor"> #endif</span>00163 <span class="preprocessor"></span> <span class="comment">// schedule the next bit</span>00164 outb(OCR2, inb(TCNT2) + UartswBaudRateDiv);00165 <span class="comment">// enable OC2 interrupt</span>00166 sbi(TIMSK, OCIE2);00167 }00168 <span class="comment"></span>00169 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span><a name="l00170"></a><a class="code" href="uartsw2_8c.html#a15">00170</a> <span class="comment"></span>u08 <a class="code" href="uartsw_8c.html#a15">uartswReceiveByte</a>(u08* rxData)00171 {00172 <span class="comment">// make sure we have a receive buffer</span>00173 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o1">size</a>)00174 {00175 <span class="comment">// make sure we have data</span>00176 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)00177 {00178 <span class="comment">// get byte from beginning of buffer</span>00179 *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartswRxBuffer);00180 <span class="keywordflow">return</span> TRUE;00181 }00182 <span class="keywordflow">else</span>00183 {00184 <span class="comment">// no data</span>00185 <span class="keywordflow">return</span> FALSE;00186 }00187 }00188 <span class="keywordflow">else</span>00189 {00190 <span class="comment">// no buffer</span>00191 <span class="keywordflow">return</span> FALSE;00192 }00193 }00194 <a name="l00195"></a><a class="code" href="uartsw2_8c.html#a16">00195</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>(<span class="keywordtype">void</span>)00196 {00197 <span class="keywordflow">if</span>(UartswTxBitNum)00198 {00199 <span class="comment">// there are bits still waiting to be transmitted</span>00200 <span class="keywordflow">if</span>(UartswTxBitNum > 1)00201 {00202 <span class="comment">// transmit data bits (inverted, LSB first)</span>00203 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00204 <span class="preprocessor"></span> <span class="keywordflow">if</span>( !(UartswTxData & 0x01) )00205 <span class="preprocessor"> #else</span>00206 <span class="preprocessor"></span> <span class="keywordflow">if</span>( (UartswTxData & 0x01) )00207 <span class="preprocessor"> #endif</span>00208 <span class="preprocessor"></span> sbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00209 <span class="keywordflow">else</span>00210 cbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00211 <span class="comment">// shift bits down</span>00212 UartswTxData = UartswTxData>>1;00213 }00214 <span class="keywordflow">else</span>00215 {00216 <span class="comment">// transmit stop bit</span>00217 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00218 <span class="preprocessor"></span> cbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00219 <span class="preprocessor"> #else</span>00220 <span class="preprocessor"></span> sbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00221 <span class="preprocessor"> #endif</span>00222 <span class="preprocessor"></span> }00223 <span class="comment">// schedule the next bit</span>00224 outb(OCR2, inb(OCR2) + UartswBaudRateDiv);00225 <span class="comment">// count down</span>00226 UartswTxBitNum--;00227 }00228 <span class="keywordflow">else</span>00229 {00230 <span class="comment">// transmission is done</span>00231 <span class="comment">// clear busy flag</span>00232 UartswTxBusy = FALSE;00233 <span class="comment">// disable OC2 interrupt</span>00234 cbi(TIMSK, OCIE2);00235 }00236 }00237 <a name="l00238"></a><a class="code" href="uartsw2_8c.html#a17">00238</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>(<span class="keywordtype">void</span>)00239 {00240 <span class="comment">// this function runs on either:</span>00241 <span class="comment">// - a rising edge interrupt</span>00242 <span class="comment">// - Timer 0 output compare</span>00243 <span class="keywordflow">if</span>(!UartswRxBusy)00244 {00245 <span class="comment">// UART was not previously busy,</span>00246 <span class="comment">// this must be is a start bit</span>00247 00248 <span class="comment">// disable INT2 interrupt</span>00249 cbi(GICR, INT2);00250 <span class="comment">// schedule data bit sampling 1.5 bit periods from now</span>00251 outb(OCR0, inb(TCNT0) + UartswBaudRateDiv + UartswBaudRateDiv/2);00252 <span class="comment">// clear OC0 interrupt flag</span>00253 sbi(TIFR, OCF0);00254 <span class="comment">// enable OC0 interrupt</span>00255 sbi(TIMSK, OCIE0);00256 <span class="comment">// set busy flag</span>00257 UartswRxBusy = TRUE;00258 <span class="comment">// reset bit counter</span>00259 UartswRxBitNum = 0;00260 <span class="comment">// reset data</span>00261 UartswRxData = 0;00262 }00263 <span class="keywordflow">else</span>00264 {00265 <span class="comment">// start bit has already been received</span>00266 <span class="comment">// we're in the data bits</span>00267 00268 <span class="comment">// shift data byte to make room for new bit</span>00269 UartswRxData = UartswRxData>>1;00270 00271 <span class="comment">// sample the data line</span>00272 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00273 <span class="preprocessor"></span> <span class="keywordflow">if</span>( !(inb(<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PORTIN</a>) & (1<<<a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>)) )00274 <span class="preprocessor"> #else</span>00275 <span class="preprocessor"></span> <span class="keywordflow">if</span>( (inb(<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PORTIN</a>) & (1<<<a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>)) )00276 <span class="preprocessor"> #endif</span>00277 <span class="preprocessor"></span> {00278 <span class="comment">// serial line is marking</span>00279 <span class="comment">// record '1' bit</span>00280 UartswRxData |= 0x80;00281 }00282 00283 <span class="comment">// increment bit counter</span>00284 UartswRxBitNum++;00285 <span class="comment">// schedule next bit sample</span>00286 outb(OCR0, inb(OCR0) + UartswBaudRateDiv);00287 00288 <span class="comment">// check if we have a full byte</span>00289 <span class="keywordflow">if</span>(UartswRxBitNum >= 8)00290 {00291 <span class="comment">// save data in receive buffer</span>00292 <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&uartswRxBuffer, UartswRxData);00293 <span class="comment">// disable OC0 interrupt</span>00294 cbi(TIMSK, OCIE0);00295 <span class="comment">// clear INT2 interrupt flag</span>00296 sbi(GIFR, INTF2);00297 <span class="comment">// enable INT interrupt</span>00298 sbi(GICR, INT2);00299 <span class="comment">// clear busy flag</span>00300 UartswRxBusy = FALSE;00301 }00302 }00303 }00304 00305 <a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_INTERRUPT2)00306 {00307 <span class="comment">// run RxBit service routine</span>00308 <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>();00309 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Aug 22 04:29:28 2005 for Procyon AVRlib by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -