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

📄 uart2_8c-source.html

📁 avr cpu 库源代码 对avr单片机编程很有帮助
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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>Procyon AVRlib: uart2.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</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>uart2.c</h1><a href="uart2_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uart2.c \brief Dual UART driver with buffer support. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'uart2.c'</span>00005 <span class="comment">// Title        : Dual UART driver with buffer support</span>00006 <span class="comment">// Author       : Pascal Stang - Copyright (C) 2000-2004</span>00007 <span class="comment">// Created      : 11/20/2000</span>00008 <span class="comment">// Revised      : 07/04/2004</span>00009 <span class="comment">// Version      : 1.0</span>00010 <span class="comment">// Target MCU   : ATMEL AVR Series</span>00011 <span class="comment">// Editor Tabs  : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// Description  : This is a UART driver for AVR-series processors with two</span>00014 <span class="comment">//      hardware UARTs such as the mega161 and mega128 </span>00015 <span class="comment">//</span>00016 <span class="comment">// This code is distributed under the GNU Public License</span>00017 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>00018 <span class="comment">//</span>00019 <span class="comment">//*****************************************************************************</span>00020 00021 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>00022 <span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>00023 00024 <span class="preprocessor">#include "<a class="code" href="buffer_8h.html">buffer.h</a>"</span>00025 <span class="preprocessor">#include "<a class="code" href="uart2_8h.html">uart2.h</a>"</span>00026 00027 <span class="comment">// UART global variables</span>00028 <span class="comment">// flag variables</span>00029 <span class="keyword">volatile</span> u08   <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[2];00030 <span class="keyword">volatile</span> u08   <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[2];00031 <span class="comment">// receive and transmit buffers</span>00032 <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a2">uartRxBuffer</a>[2];00033 <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a3">uartTxBuffer</a>[2];00034 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[2];00035 <span class="preprocessor">#ifndef UART_BUFFER_EXTERNAL_RAM</span>00036 <span class="preprocessor"></span>    <span class="comment">// using internal ram,</span>00037     <span class="comment">// automatically allocate space in ram for each buffer</span>00038     <span class="keyword">static</span> <span class="keywordtype">char</span> uart0RxData[<a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>];00039     <span class="keyword">static</span> <span class="keywordtype">char</span> uart0TxData[<a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>];00040     <span class="keyword">static</span> <span class="keywordtype">char</span> uart1RxData[<a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>];00041     <span class="keyword">static</span> <span class="keywordtype">char</span> uart1TxData[<a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>];00042 <span class="preprocessor">#endif</span>00043 <span class="preprocessor"></span>00044 <span class="keyword">typedef</span> void (*voidFuncPtru08)(<span class="keywordtype">unsigned</span> char);00045 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtru08 UartRxFunc[2];00046 <a name="l00047"></a><a class="code" href="group__uart.html#ga0">00047</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga0">uartInit</a>(<span class="keywordtype">void</span>)00048 {00049     <span class="comment">// initialize both uarts</span>00050     <a class="code" href="group__uart2.html#ga1">uart0Init</a>();00051     <a class="code" href="group__uart2.html#ga2">uart1Init</a>();00052 }00053 <a name="l00054"></a><a class="code" href="group__uart2.html#ga1">00054</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga1">uart0Init</a>(<span class="keywordtype">void</span>)00055 {00056     <span class="comment">// initialize the buffers</span>00057     <a class="code" href="group__uart2.html#ga3">uart0InitBuffers</a>();00058     <span class="comment">// initialize user receive handlers</span>00059     UartRxFunc[0] = 0;00060     <span class="comment">// enable RxD/TxD and interrupts</span>00061     outb(UCSR0B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));00062     <span class="comment">// set default baud rate</span>00063     <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(0, <a class="code" href="group__uart2.html#ga24">UART0_DEFAULT_BAUD_RATE</a>); 00064     <span class="comment">// initialize states</span>00065     <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[0] = TRUE;00066     <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[0] = FALSE;00067     <span class="comment">// clear overflow count</span>00068     <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[0] = 0;00069     <span class="comment">// enable interrupts</span>00070     sei();00071 }00072 <a name="l00073"></a><a class="code" href="group__uart2.html#ga2">00073</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga2">uart1Init</a>(<span class="keywordtype">void</span>)00074 {00075     <span class="comment">// initialize the buffers</span>00076     uart1InitBuffers();00077     <span class="comment">// initialize user receive handlers</span>00078     UartRxFunc[1] = 0;00079     <span class="comment">// enable RxD/TxD and interrupts</span>00080     outb(UCSR1B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));00081     <span class="comment">// set default baud rate</span>00082     <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(1, <a class="code" href="group__uart2.html#ga25">UART1_DEFAULT_BAUD_RATE</a>);00083     <span class="comment">// initialize states</span>00084     <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[1] = TRUE;00085     <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[1] = FALSE;00086     <span class="comment">// clear overflow count</span>00087     <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[1] = 0;00088     <span class="comment">// enable interrupts</span>00089     sei();00090 }00091 <a name="l00092"></a><a class="code" href="group__uart2.html#ga3">00092</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga3">uart0InitBuffers</a>(<span class="keywordtype">void</span>)00093 {00094 <span class="preprocessor">    #ifndef UART_BUFFER_EXTERNAL_RAM</span>00095 <span class="preprocessor"></span>        <span class="comment">// initialize the UART0 buffers</span>00096         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartRxBuffer[0], uart0RxData, <a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>);00097         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartTxBuffer[0], uart0TxData, <a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>);00098 <span class="preprocessor">    #else</span>00099 <span class="preprocessor"></span>        <span class="comment">// initialize the UART0 buffers</span>00100         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartRxBuffer[0], (u08*) UART0_RX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>);00101         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartTxBuffer[0], (u08*) UART0_TX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>);00102 <span class="preprocessor">    #endif</span>00103 <span class="preprocessor"></span>}00104 00105 <span class="keywordtype">void</span> uart1InitBuffers(<span class="keywordtype">void</span>)00106 {00107 <span class="preprocessor">    #ifndef UART_BUFFER_EXTERNAL_RAM</span>00108 <span class="preprocessor"></span>        <span class="comment">// initialize the UART1 buffers</span>00109         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartRxBuffer[1], uart1RxData, <a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>);00110         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartTxBuffer[1], uart1TxData, <a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>);00111 <span class="preprocessor">    #else</span>00112 <span class="preprocessor"></span>        <span class="comment">// initialize the UART1 buffers</span>00113         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartRxBuffer[1], (u08*) UART1_RX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>);00114         <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartTxBuffer[1], (u08*) UART1_TX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>);00115 <span class="preprocessor">    #endif</span>00116 <span class="preprocessor"></span>}00117 <a name="l00118"></a><a class="code" href="group__uart2.html#ga5">00118</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga2">uartSetRxHandler</a>(u08 nUart, <span class="keywordtype">void</span> (*rx_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c))00119 {00120     <span class="comment">// make sure the uart number is within bounds</span>00121     <span class="keywordflow">if</span>(nUart &lt; 2)00122     {00123         <span class="comment">// set the receive interrupt to run the supplied user function</span>00124         UartRxFunc[nUart] = rx_func;00125     }00126 }00127 <a name="l00128"></a><a class="code" href="group__uart2.html#ga6">00128</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(u08 nUart, u32 baudrate)00129 {00130     <span class="comment">// calculate division factor for requested baud rate, and set it</span>00131     u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);00132     <span class="keywordflow">if</span>(nUart)00133     {00134         outb(UBRR1L, bauddiv);00135 <span class="preprocessor">        #ifdef UBRR1H</span>00136 <span class="preprocessor"></span>        outb(UBRR1H, bauddiv&gt;&gt;8);00137 <span class="preprocessor">        #endif</span>00138 <span class="preprocessor"></span>    }00139     <span class="keywordflow">else</span>00140     {00141         outb(UBRR0L, bauddiv);00142 <span class="preprocessor">        #ifdef UBRR0H</span>00143 <span class="preprocessor"></span>        outb(UBRR0H, bauddiv&gt;&gt;8);00144 <span class="preprocessor">        #endif</span>00145 <span class="preprocessor"></span>    }00146 }00147 <a name="l00148"></a><a class="code" href="group__uart2.html#ga7">00148</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga4">uartGetRxBuffer</a>(u08 nUart)00149 {00150     <span class="comment">// return rx buffer pointer</span>00151     <span class="keywordflow">return</span> &amp;uartRxBuffer[nUart];00152 }00153 <a name="l00154"></a><a class="code" href="group__uart2.html#ga8">00154</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga5">uartGetTxBuffer</a>(u08 nUart)00155 {00156     <span class="comment">// return tx buffer pointer</span>00157     <span class="keywordflow">return</span> &amp;uartTxBuffer[nUart];00158 }00159 <a name="l00160"></a><a class="code" href="group__uart2.html#ga9">00160</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga6">uartSendByte</a>(u08 nUart, u08 txData)00161 {00162     <span class="comment">// wait for the transmitter to be ready</span>00163 <span class="comment">//  while(!uartReadyTx[nUart]);</span>00164     <span class="comment">// send byte</span>00165     <span class="keywordflow">if</span>(nUart)00166     {00167         <span class="keywordflow">while</span>(!(UCSR1A &amp; (1&lt;&lt;UDRE)));00168         outb(UDR1, txData);00169     }00170     <span class="keywordflow">else</span>00171     {00172         <span class="keywordflow">while</span>(!(UCSR0A &amp; (1&lt;&lt;UDRE)));00173         outb(UDR0, txData);00174     }00175     <span class="comment">// set ready state to FALSE</span>00176     <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[nUart] = FALSE;00177 }00178 <a name="l00179"></a><a class="code" href="group__uart2.html#ga10">00179</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga10">uart0SendByte</a>(u08 data)00180 {00181     <span class="comment">// send byte on UART0</span>00182     <a class="code" href="group__uart.html#ga6">uartSendByte</a>(0, data);00183 }00184 00185 <span class="keywordtype">void</span> uart1SendByte(u08 data)00186 {00187     <span class="comment">// send byte on UART1</span>00188     <a class="code" href="group__uart.html#ga6">uartSendByte</a>(1, data);00189 }

⌨️ 快捷键说明

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