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

📄 uart_8c-source.html

📁 使用AVR单片机控制步进电机的软件代码
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>AVR446 - Linear speed control of stepper motor: uart.c Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.7 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div><h1>uart.c</h1><a href="uart_8c.html">Go to the documentation of this file.</a><pre class="fragment"><div>00001 <span class="comment">/*This file has been prepared for Doxygen automatic documentation generation.*/</span>00020 <span class="preprocessor">#include &lt;ioavr.h&gt;</span>00021 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00022 <span class="preprocessor">#include "<a class="code" href="uart_8h.html">uart.h</a>"</span>00023 <span class="preprocessor">#include "<a class="code" href="sm__driver_8h.html">sm_driver.h</a>"</span>00024 <span class="preprocessor">#include "<a class="code" href="speed__cntr_8h.html">speed_cntr.h</a>"</span>00025 <a name="l00027"></a><a class="code" href="uart_8c.html#a1">00027</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="uart_8c.html#a1">UART_RxBuffer</a>[<a class="code" href="uart_8h.html#a0">UART_RX_BUFFER_SIZE</a>];<a name="l00029"></a><a class="code" href="uart_8c.html#a2">00029</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="uart_8c.html#a2">UART_RxPtr</a>;00030 00031 <span class="comment">// Static Variables.</span>00033 <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> UART_TxBuffer[<a class="code" href="uart_8h.html#a2">UART_TX_BUFFER_SIZE</a>];00035 <span class="keyword">static</span> <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> UART_TxHead;00037 <span class="keyword">static</span> <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> UART_TxTail;00038 <a name="l00045"></a><a class="code" href="uart_8h.html#a7">00045</a> <span class="keywordtype">void</span> <a class="code" href="uart_8c.html#a6">InitUART</a>(<span class="keywordtype">void</span>)00046 {00047   <span class="comment">// Set baud rate. 19.2 kbps trasfer speed running at 8 MHz.</span>00048 <span class="comment">//#define BAUD 25</span>00049   <span class="comment">// Set baud rate. 19.2 kbps trasfer speed running at 3.6864 MHz.</span>00050 <span class="preprocessor">#define BAUD 11</span>00051 <span class="preprocessor"></span>00052   UBRR0H = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)(<a class="code" href="uart_8c.html#a0">BAUD</a>&gt;&gt;8);00053   UBRR0L = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)<a class="code" href="uart_8c.html#a0">BAUD</a>;00054   <span class="comment">/* Enable receiver and transmitter, rx and tx int */</span>00055   UCSR0B = (1&lt;&lt;RXEN0)|(1&lt;&lt;TXEN0)|(1&lt;&lt;RXCIE0)|(1&lt;&lt;TXCIE0);00056   <span class="comment">/* Set frame format: 8data, 1stop bit */</span>00057   UCSR0C = (3&lt;&lt;UCSZ00);00058 00059   <span class="comment">// Flush Buffers</span>00060   <a class="code" href="uart_8c.html#a2">UART_RxPtr</a> = 0;00061   UART_TxTail = 0;00062   UART_TxHead = 0;00063 }00064 00065 <a name="l00073"></a><a class="code" href="uart_8h.html#a8">00073</a> <span class="keywordtype">void</span> <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00074 {00075   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> tmphead;00076 00077   <span class="comment">// Calculate buffer index</span>00078   tmphead = ( UART_TxHead + 1 ) &amp; <a class="code" href="uart_8h.html#a3">UART_TX_BUFFER_MASK</a>;00079   <span class="comment">// Wait for free space in buffer</span>00080   <span class="keywordflow">while</span> ( tmphead == UART_TxTail )00081     ;00082   <span class="comment">// Store data in buffer</span>00083   UART_TxBuffer[tmphead] = data;00084   <span class="comment">// Store new index</span>00085   UART_TxHead = tmphead;00086   <span class="comment">// Enable UDRE interrupt</span>00087   <a class="code" href="uart_8h.html#a4">SET_UDRIE</a>;00088 }00089 <a name="l00097"></a><a class="code" href="uart_8h.html#a9">00097</a> <span class="keywordtype">void</span> <a class="code" href="uart_8h.html#a9">uart_SendString</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> Str[])00098 {00099   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> n = 0;00100   <span class="keywordflow">while</span>(Str[n])00101     <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(Str[n++]);00102 }00103 <a name="l00111"></a><a class="code" href="uart_8h.html#a10">00111</a> <span class="keywordtype">void</span> <a class="code" href="uart_8h.html#a10">uart_SendInt</a>(<span class="keywordtype">int</span> x)00112 {00113   <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> dec[] = <span class="stringliteral">"0123456789"</span>;00114   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> div_val = 10000;00115 00116   <span class="keywordflow">if</span> (x &lt; 0){00117     x = - x;00118     <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(<span class="charliteral">'-'</span>);00119   }00120   <span class="keywordflow">while</span> (div_val &gt; 1 &amp;&amp; div_val &gt; x)00121     div_val /= 10;00122   <span class="keywordflow">do</span>{00123     <a class="code" href="uart_8h.html#a8">uart_SendByte</a> (dec[x / div_val]);00124     x %= div_val;00125     div_val /= 10;00126   }<span class="keywordflow">while</span>(div_val);00127 }00128 <a name="l00135"></a><a class="code" href="uart_8h.html#a11">00135</a> <span class="keywordtype">void</span> <a class="code" href="uart_8c.html#a10">uart_FlushRxBuffer</a>(<span class="keywordtype">void</span>){00136   <a class="code" href="uart_8c.html#a2">UART_RxPtr</a> = 0;00137   <a class="code" href="uart_8c.html#a1">UART_RxBuffer</a>[<a class="code" href="uart_8c.html#a2">UART_RxPtr</a>] = 0;00138 }00139 00145 <span class="preprocessor">#pragma vector=USART_RX_vect</span><a name="l00146"></a><a class="code" href="uart_8h.html#a12">00146</a> <span class="preprocessor"></span>__interrupt <span class="keywordtype">void</span> <a class="code" href="uart_8c.html#a11">UART_RX_interrupt</a>( <span class="keywordtype">void</span> )00147 {00148   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data;00149 00150   <span class="comment">// Read the received data.</span>00151   data = UDR0;00152 00153   <span class="keywordflow">if</span>(<a class="code" href="main_8c.html#a0">status</a>.<a class="code" href="structGLOBAL__FLAGS.html#o2">running</a> == <a class="code" href="global_8h.html#a1">FALSE</a>){00154     <span class="comment">// If backspace.</span>00155     <span class="keywordflow">if</span>(data == <span class="charliteral">'\b'</span>)00156     {00157       <span class="keywordflow">if</span>(<a class="code" href="uart_8c.html#a2">UART_RxPtr</a>)00158       <span class="comment">// Done if not at beginning of buffer.</span>00159       {00160         <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(<span class="charliteral">'\b'</span>);00161         <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(<span class="charliteral">' '</span>);00162         <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(<span class="charliteral">'\b'</span>);00163         <a class="code" href="uart_8c.html#a2">UART_RxPtr</a>--;00164         <a class="code" href="uart_8c.html#a1">UART_RxBuffer</a>[<a class="code" href="uart_8c.html#a2">UART_RxPtr</a>]=0x00;00165       }00166     }00167     <span class="comment">// Not backspace.</span>00168     <span class="keywordflow">else</span>00169     {00170       <span class="comment">// Put the data into RxBuf</span>00171       <span class="comment">// and place 0x00 after it. If buffer is full,</span>00172       <span class="comment">// data is written to UART_RX_BUFFER_SIZE - 1.</span>00173       <span class="keywordflow">if</span>(<a class="code" href="uart_8c.html#a2">UART_RxPtr</a> &lt; (<a class="code" href="uart_8h.html#a0">UART_RX_BUFFER_SIZE</a> - 1)){00174         <a class="code" href="uart_8c.html#a1">UART_RxBuffer</a>[<a class="code" href="uart_8c.html#a2">UART_RxPtr</a>] = data;00175         <a class="code" href="uart_8c.html#a1">UART_RxBuffer</a>[<a class="code" href="uart_8c.html#a2">UART_RxPtr</a> + 1]=0x00;00176         <a class="code" href="uart_8c.html#a2">UART_RxPtr</a>++;00177       }00178       <span class="keywordflow">else</span>00179       {00180         <a class="code" href="uart_8c.html#a1">UART_RxBuffer</a>[<a class="code" href="uart_8c.html#a2">UART_RxPtr</a> - 1] = data;00181         <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(<span class="charliteral">'\b'</span>);00182       }00183       <span class="comment">// If enter.</span>00184       <span class="keywordflow">if</span>(data == 13){00185         <a class="code" href="main_8c.html#a0">status</a>.<a class="code" href="structGLOBAL__FLAGS.html#o0">cmd</a> = <a class="code" href="global_8h.html#a0">TRUE</a>;00186       }00187       <span class="keywordflow">else</span>00188         <a class="code" href="uart_8h.html#a8">uart_SendByte</a>(data);00189     }00190   }00191 }00192 00199 <span class="preprocessor">#pragma vector=USART_UDRE_vect</span><a name="l00200"></a><a class="code" href="uart_8h.html#a13">00200</a> <span class="preprocessor"></span>__interrupt <span class="keywordtype">void</span> <a class="code" href="uart_8c.html#a12">UART_TX_interrupt</a>( <span class="keywordtype">void</span> )00201 {00202   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> UART_TxTail_tmp;00203    UART_TxTail_tmp = UART_TxTail;00204 00205   <span class="comment">// Check if all data is transmitted</span>00206   <span class="keywordflow">if</span> ( UART_TxHead !=  UART_TxTail_tmp )00207   {00208     <span class="comment">// Calculate buffer index</span>00209     UART_TxTail_tmp = ( UART_TxTail + 1 ) &amp; <a class="code" href="uart_8h.html#a3">UART_TX_BUFFER_MASK</a>;00210     <span class="comment">// Store new index</span>00211     UART_TxTail =  UART_TxTail_tmp;00212     <span class="comment">// Start transmition</span>00213     UDR0= UART_TxBuffer[ UART_TxTail_tmp];00214   }00215   <span class="keywordflow">else</span>00216     <span class="comment">// Disable UDRE interrupt</span>00217     <a class="code" href="uart_8h.html#a5">CLR_UDRIE</a>;00218 }</div></pre><hr size="1"><address style="align: right;"><small>Generated on Mon May 8 15:05:03 2006 for AVR446 - Linear speed control of stepper motor by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address></body></html>

⌨️ 快捷键说明

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