📄 uartsw2_8c-source.html
字号:
00143 <span class="comment">// save data</span>00144 UartswTxData = data;00145 <span class="comment">// set number of bits (+1 for stop bit)</span>00146 UartswTxBitNum = 9;00147 00148 <span class="comment">// set the start bit</span>00149 sbi(<a class="code" href="uartsw2conf_8h.html#a1">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a3">UARTSW_TX_PIN</a>);00150 <span class="comment">// schedule the next bit</span>00151 outb(OCR2, inb(TCNT2) + UartswBaudRateDiv);00152 <span class="comment">// enable OC2 interrupt</span>00153 sbi(TIMSK, OCIE2);00154 }00155 <span class="comment"></span>00156 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span><a name="l00157"></a><a class="code" href="uartsw2_8c.html#a15">00157</a> <span class="comment"></span>u08 <a class="code" href="uartsw2_8h.html#a6">uartswReceiveByte</a>(u08* rxData)00158 {00159 <span class="comment">// make sure we have a receive buffer</span>00160 <span class="keywordflow">if</span>(uartswRxBuffer.size)00161 {00162 <span class="comment">// make sure we have data</span>00163 <span class="keywordflow">if</span>(uartswRxBuffer.datalength)00164 {00165 <span class="comment">// get byte from beginning of buffer</span>00166 *rxData = <a class="code" href="buffer_8h.html#a2">bufferGetFromFront</a>(&uartswRxBuffer);00167 <span class="keywordflow">return</span> TRUE;00168 }00169 <span class="keywordflow">else</span>00170 {00171 <span class="comment">// no data</span>00172 <span class="keywordflow">return</span> FALSE;00173 }00174 }00175 <span class="keywordflow">else</span>00176 {00177 <span class="comment">// no buffer</span>00178 <span class="keywordflow">return</span> FALSE;00179 }00180 }00181 <a name="l00182"></a><a class="code" href="uartsw2_8c.html#a16">00182</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>(<span class="keywordtype">void</span>)00183 {00184 <span class="keywordflow">if</span>(UartswTxBitNum)00185 {00186 <span class="comment">// there are bits still waiting to be transmitted</span>00187 <span class="keywordflow">if</span>(UartswTxBitNum > 1)00188 {00189 <span class="comment">// transmit data bits (inverted, LSB first)</span>00190 <span class="keywordflow">if</span>( !(UartswTxData & 0x01) )00191 sbi(<a class="code" href="uartsw2conf_8h.html#a1">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a3">UARTSW_TX_PIN</a>);00192 <span class="keywordflow">else</span>00193 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>);00194 <span class="comment">// shift bits down</span>00195 UartswTxData = UartswTxData>>1;00196 }00197 <span class="keywordflow">else</span>00198 {00199 <span class="comment">// transmit stop bit</span>00200 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>);00201 }00202 <span class="comment">// schedule the next bit</span>00203 outb(OCR2, inb(OCR2) + UartswBaudRateDiv);00204 <span class="comment">// count down</span>00205 UartswTxBitNum--;00206 }00207 <span class="keywordflow">else</span>00208 {00209 <span class="comment">// transmission is done</span>00210 <span class="comment">// clear busy flag</span>00211 UartswTxBusy = FALSE;00212 <span class="comment">// disable OC2 interrupt</span>00213 cbi(TIMSK, OCIE2);00214 }00215 }00216 <a name="l00217"></a><a class="code" href="uartsw2_8c.html#a17">00217</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>(<span class="keywordtype">void</span>)00218 {00219 <span class="comment">// this function runs on either:</span>00220 <span class="comment">// - a rising edge interrupt</span>00221 <span class="comment">// - Timer 0 output compare</span>00222 <span class="keywordflow">if</span>(!UartswRxBusy)00223 {00224 <span class="comment">// UART was not previously busy,</span>00225 <span class="comment">// this must be is a start bit</span>00226 00227 <span class="comment">// disable INT2 interrupt</span>00228 cbi(GICR, INT2);00229 <span class="comment">// schedule data bit sampling 1.5 bit periods from now</span>00230 outb(OCR0, inb(TCNT0) + UartswBaudRateDiv + UartswBaudRateDiv/2);00231 <span class="comment">// clear OC0 interrupt flag</span>00232 sbi(TIFR, OCF0);00233 <span class="comment">// enable OC0 interrupt</span>00234 sbi(TIMSK, OCIE0);00235 <span class="comment">// set busy flag</span>00236 UartswRxBusy = TRUE;00237 <span class="comment">// reset bit counter</span>00238 UartswRxBitNum = 0;00239 <span class="comment">// reset data</span>00240 UartswRxData = 0;00241 }00242 <span class="keywordflow">else</span>00243 {00244 <span class="comment">// start bit has already been received</span>00245 <span class="comment">// we're in the data bits</span>00246 00247 <span class="comment">// shift data byte to make room for new bit</span>00248 UartswRxData = UartswRxData>>1;00249 00250 <span class="comment">// sample the data line</span>00251 <span class="keywordflow">if</span>( !(inb(<a class="code" href="uartsw2conf_8h.html#a6">UARTSW_RX_PORTIN</a>) & (1<<<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PIN</a>)) )00252 {00253 <span class="comment">// serial line is low</span>00254 <span class="comment">// record '1' bit (data inverted)</span>00255 UartswRxData |= 0x80;00256 }00257 00258 <span class="comment">// increment bit counter</span>00259 UartswRxBitNum++;00260 <span class="comment">// schedule next bit sample</span>00261 outb(OCR0, inb(OCR0) + UartswBaudRateDiv);00262 00263 <span class="comment">// check if we have a full byte</span>00264 <span class="keywordflow">if</span>(UartswRxBitNum >= 8)00265 {00266 <span class="comment">// save data in receive buffer</span>00267 <a class="code" href="buffer_8h.html#a5">bufferAddToEnd</a>(&uartswRxBuffer, UartswRxData);00268 <span class="comment">// disable OC0 interrupt</span>00269 cbi(TIMSK, OCIE0);00270 <span class="comment">// clear INT2 interrupt flag</span>00271 sbi(GIFR, INTF2);00272 <span class="comment">// enable INT interrupt</span>00273 sbi(GICR, INT2);00274 <span class="comment">// clear busy flag</span>00275 UartswRxBusy = FALSE;00276 }00277 }00278 }00279 00280 <a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_INTERRUPT2)00281 {00282 <span class="comment">// run RxBit service routine</span>00283 <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>();00284 }</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 + -