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

📄 uart2_8c-source.html

📁 单片机的软件
💻 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></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 <span class="preprocessor">#include &lt;avr/signal.h&gt;</span>00024 00025 <span class="preprocessor">#include "<a class="code" href="buffer_8h.html">buffer.h</a>"</span>00026 <span class="preprocessor">#include "<a class="code" href="uart2_8h.html">uart2.h</a>"</span>00027 00028 <span class="comment">// UART global variables</span>00029 <span class="comment">// flag variables</span>00030 <span class="keyword">volatile</span> u08   <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[2];00031 <span class="keyword">volatile</span> u08   <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[2];00032 <span class="comment">// receive and transmit buffers</span>00033 <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a2">uartRxBuffer</a>[2];00034 <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a3">uartTxBuffer</a>[2];00035 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[2];00036 <span class="preprocessor">#ifndef UART_BUFFERS_EXTERNAL_RAM</span>00037 <span class="preprocessor"></span>    <span class="comment">// using internal ram,</span>00038     <span class="comment">// automatically allocate space in ram for each buffer</span>00039     <span class="keyword">static</span> <span class="keywordtype">char</span> uart0RxData[<a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>];00040     <span class="keyword">static</span> <span class="keywordtype">char</span> uart0TxData[<a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>];00041     <span class="keyword">static</span> <span class="keywordtype">char</span> uart1RxData[<a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>];00042     <span class="keyword">static</span> <span class="keywordtype">char</span> uart1TxData[<a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>];00043 <span class="preprocessor">#endif</span>00044 <span class="preprocessor"></span>00045 <span class="keyword">typedef</span> void (*voidFuncPtru08)(<span class="keywordtype">unsigned</span> char);00046 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtru08 UartRxFunc[2];00047 <a name="l00048"></a><a class="code" href="group__uart.html#ga0">00048</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga0">uartInit</a>(<span class="keywordtype">void</span>)00049 {00050     <span class="comment">// initialize both uarts</span>00051     <a class="code" href="group__uart2.html#ga1">uart0Init</a>();00052     <a class="code" href="group__uart2.html#ga2">uart1Init</a>();00053 }00054 <a name="l00055"></a><a class="code" href="group__uart2.html#ga1">00055</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga1">uart0Init</a>(<span class="keywordtype">void</span>)00056 {00057     <span class="comment">// initialize the buffers</span>00058     <a class="code" href="group__uart2.html#ga3">uart0InitBuffers</a>();00059     <span class="comment">// initialize user receive handlers</span>00060     UartRxFunc[0] = 0;00061     <span class="comment">// enable RxD/TxD and interrupts</span>00062     outb(UCSR0B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));00063     <span class="comment">// set default baud rate</span>00064     <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(0, <a class="code" href="group__uart2.html#ga24">UART0_DEFAULT_BAUD_RATE</a>); 00065     <span class="comment">// initialize states</span>00066     <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[0] = TRUE;00067     <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[0] = FALSE;00068     <span class="comment">// clear overflow count</span>00069     <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[0] = 0;00070     <span class="comment">// enable interrupts</span>00071     sei();00072 }00073 <a name="l00074"></a><a class="code" href="group__uart2.html#ga2">00074</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga2">uart1Init</a>(<span class="keywordtype">void</span>)00075 {00076     <span class="comment">// initialize the buffers</span>00077     uart1InitBuffers();00078     <span class="comment">// initialize user receive handlers</span>00079     UartRxFunc[1] = 0;00080     <span class="comment">// enable RxD/TxD and interrupts</span>00081     outb(UCSR1B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));00082     <span class="comment">// set default baud rate</span>00083     <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(1, <a class="code" href="group__uart2.html#ga25">UART1_DEFAULT_BAUD_RATE</a>);00084     <span class="comment">// initialize states</span>00085     <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[1] = TRUE;00086     <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[1] = FALSE;00087     <span class="comment">// clear overflow count</span>00088     <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[1] = 0;00089     <span class="comment">// enable interrupts</span>00090     sei();00091 }00092 <a name="l00093"></a><a class="code" href="group__uart2.html#ga3">00093</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga3">uart0InitBuffers</a>(<span class="keywordtype">void</span>)00094 {00095 <span class="preprocessor">    #ifndef UART_BUFFERS_EXTERNAL_RAM</span>00096 <span class="preprocessor"></span>        <span class="comment">// initialize the UART0 buffers</span>00097         <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>);00098         <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>);00099 <span class="preprocessor">    #else</span>00100 <span class="preprocessor"></span>        <span class="comment">// initialize the UART0 buffers</span>00101         <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>);00102         <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>);00103 <span class="preprocessor">    #endif</span>00104 <span class="preprocessor"></span>}00105 00106 <span class="keywordtype">void</span> uart1InitBuffers(<span class="keywordtype">void</span>)00107 {00108 <span class="preprocessor">    #ifndef UART_BUFFERS_EXTERNAL_RAM</span>00109 <span class="preprocessor"></span>        <span class="comment">// initialize the UART1 buffers</span>00110         <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>);00111         <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>);00112 <span class="preprocessor">    #else</span>00113 <span class="preprocessor"></span>        <span class="comment">// initialize the UART1 buffers</span>00114         <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>);00115         <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>);00116 <span class="preprocessor">    #endif</span>00117 <span class="preprocessor"></span>}00118 <a name="l00119"></a><a class="code" href="group__uart2.html#ga5">00119</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))00120 {00121     <span class="comment">// make sure the uart number is within bounds</span>00122     <span class="keywordflow">if</span>(nUart &lt; 2)00123     {00124         <span class="comment">// set the receive interrupt to run the supplied user function</span>00125         UartRxFunc[nUart] = rx_func;00126     }00127 }00128 <a name="l00129"></a><a class="code" href="group__uart2.html#ga6">00129</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(u08 nUart, u32 baudrate)00130 {00131     <span class="comment">// calculate division factor for requested baud rate, and set it</span>00132     u08 baudrateDiv;00133     baudrateDiv = (u08)((F_CPU+(baudrate*8L))/(baudrate*16L)-1);00134     <span class="keywordflow">if</span>(nUart)00135         outb(UBRR1L, baudrateDiv);00136     <span class="keywordflow">else</span>00137         outb(UBRR0L, baudrateDiv);00138 }00139 <a name="l00140"></a><a class="code" href="group__uart2.html#ga7">00140</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga4">uartGetRxBuffer</a>(u08 nUart)00141 {00142     <span class="comment">// return rx buffer pointer</span>00143     <span class="keywordflow">return</span> &amp;uartRxBuffer[nUart];00144 }00145 <a name="l00146"></a><a class="code" href="group__uart2.html#ga8">00146</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga5">uartGetTxBuffer</a>(u08 nUart)00147 {00148     <span class="comment">// return tx buffer pointer</span>00149     <span class="keywordflow">return</span> &amp;uartTxBuffer[nUart];00150 }00151 <a name="l00152"></a><a class="code" href="group__uart2.html#ga9">00152</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga6">uartSendByte</a>(u08 nUart, u08 txData)00153 {00154     <span class="comment">// wait for the transmitter to be ready</span>00155 <span class="comment">//  while(!uartReadyTx[nUart]);</span>00156     <span class="comment">// send byte</span>00157     <span class="keywordflow">if</span>(nUart)00158     {00159         <span class="keywordflow">while</span>(!(UCSR1A &amp; (1&lt;&lt;UDRE)));00160         outb(UDR1, txData);00161     }00162     <span class="keywordflow">else</span>00163     {00164         <span class="keywordflow">while</span>(!(UCSR0A &amp; (1&lt;&lt;UDRE)));00165         outb(UDR0, txData);00166     }00167     <span class="comment">// set ready state to FALSE</span>00168     <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[nUart] = FALSE;00169 }00170 <a name="l00171"></a><a class="code" href="group__uart2.html#ga10">00171</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga10">uart0SendByte</a>(u08 data)00172 {00173     <span class="comment">// send byte on UART0</span>00174     <a class="code" href="group__uart.html#ga6">uartSendByte</a>(0, data);00175 }00176 00177 <span class="keywordtype">void</span> uart1SendByte(u08 data)00178 {00179     <span class="comment">// send byte on UART1</span>00180     <a class="code" href="group__uart.html#ga6">uartSendByte</a>(1, data);00181 }00182 <a name="l00183"></a><a class="code" href="group__uart2.html#ga12">00183</a> <span class="keywordtype">int</span> <a class="code" href="group__uart2.html#ga12">uart0GetByte</a>(<span class="keywordtype">void</span>)00184 {00185     <span class="comment">// get single byte from receive buffer (if available)</span>

⌨️ 快捷键说明

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