📄 uartsw_8c-source.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>Procyon AVRlib: uartsw.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 Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>uartsw.c</h1><a href="uartsw_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uartsw.c \brief Software Interrupt-driven UART Driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'uartsw.c'</span>00005 <span class="comment">// Title : Software Interrupt-driven UART Driver</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002-2004</span>00007 <span class="comment">// Created : 7/20/2002</span>00008 <span class="comment">// Revised : 4/27/2004</span>00009 <span class="comment">// Version : 0.1</span>00010 <span class="comment">// Target MCU : Atmel AVR Series (intended for the ATmega16 and ATmega32)</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// This code is distributed under the GNU Public License</span>00014 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00015 <span class="comment">//</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#include <avr/io.h></span>00019 <span class="preprocessor">#include <avr/interrupt.h></span>00020 <span class="preprocessor">#include <avr/signal.h></span>00021 00022 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00023 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00024 <span class="preprocessor">#include "<a class="code" href="uartsw_8h.html">uartsw.h</a>"</span>00025 00026 <span class="comment">// Program ROM constants</span>00027 00028 <span class="comment">// Global variables</span>00029 00030 <span class="comment">// uartsw transmit status and data variables</span>00031 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxBusy;00032 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxData;00033 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxBitNum;00034 00035 <span class="comment">// baud rate common to transmit and receive</span>00036 <span class="keyword">static</span> <span class="keyword">volatile</span> u16 UartswBaudRateDiv;00037 00038 <span class="comment">// uartsw receive status and data variables</span>00039 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxBusy;00040 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxData;00041 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxBitNum;00042 <span class="comment">// receive buffer</span>00043 <span class="keyword">static</span> <a class="code" href="structstruct__cBuffer.html">cBuffer</a> uartswRxBuffer; <span class="comment">///< uartsw receive buffer</span>00044 <span class="comment"></span><span class="comment">// automatically allocate space in ram for each buffer</span>00045 <span class="keyword">static</span> <span class="keywordtype">char</span> uartswRxData[<a class="code" href="uartsw2conf_8h.html#a0">UARTSW_RX_BUFFER_SIZE</a>];00046 00047 <span class="comment">// functions</span>00048 <span class="comment"></span>00049 <span class="comment">//! enable and initialize the software uart</span><a name="l00050"></a><a class="code" href="uartsw2_8h.html#a0">00050</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a9">uartswInit</a>(<span class="keywordtype">void</span>)00051 {00052 <span class="comment">// initialize the buffers</span>00053 <a class="code" href="uartsw_8c.html#a10">uartswInitBuffers</a>();00054 <span class="comment">// initialize the ports</span>00055 sbi(<a class="code" href="uartsw2conf_8h.html#a3">UARTSW_TX_DDR</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>);00056 cbi(<a class="code" href="uartsw2conf_8h.html#a6">UARTSW_RX_DDR</a>, <a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>);00057 cbi(<a class="code" href="uartsw2conf_8h.html#a5">UARTSW_RX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>);00058 <span class="comment">// initialize baud rate</span>00059 <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(9600);00060 00061 <span class="comment">// setup the transmitter</span>00062 UartswTxBusy = FALSE;00063 <span class="comment">// disable OC1A interrupt</span>00064 cbi(TIMSK, OCIE1A);00065 <span class="comment">// attach TxBit service routine to OC1A</span>00066 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1OUTCOMPAREA_INT, <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>);00067 00068 <span class="comment">// setup the receiver</span>00069 UartswRxBusy = FALSE;00070 <span class="comment">// disable OC1B interrupt</span>00071 cbi(TIMSK, OCIE1B);00072 <span class="comment">// attach RxBit service routine to OC1B</span>00073 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1OUTCOMPAREB_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>);00074 <span class="comment">// attach RxBit service routine to ICP</span>00075 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1INPUTCAPTURE_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>);00076 <span class="comment">// trigger on rising edge</span>00077 sbi(TCCR1B, ICES1);00078 <span class="comment">// enable ICP interrupt</span>00079 sbi(TIMSK, TICIE1);00080 00081 <span class="comment">// turn on interrupts</span>00082 sei();00083 }00084 <span class="comment"></span>00085 <span class="comment">//! create and initialize the uart buffers</span><a name="l00086"></a><a class="code" href="uartsw2_8h.html#a1">00086</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a10">uartswInitBuffers</a>(<span class="keywordtype">void</span>)00087 {00088 <span class="comment">// initialize the UART receive buffer</span>00089 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartswRxBuffer, uartswRxData, <a class="code" href="uartsw2conf_8h.html#a0">UARTSW_RX_BUFFER_SIZE</a>);00090 }00091 <span class="comment"></span>00092 <span class="comment">//! turns off software UART</span><a name="l00093"></a><a class="code" href="uartsw2_8h.html#a2">00093</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a11">uartswOff</a>(<span class="keywordtype">void</span>)00094 {00095 <span class="comment">// disable interrupts</span>00096 cbi(TIMSK, OCIE1A);00097 cbi(TIMSK, OCIE1B);00098 cbi(TIMSK, TICIE1);00099 <span class="comment">// detach the service routines</span>00100 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1OUTCOMPAREA_INT);00101 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1OUTCOMPAREB_INT);00102 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1INPUTCAPTURE_INT);00103 }00104 <a name="l00105"></a><a class="code" href="uartsw2_8h.html#a4">00105</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(u32 baudrate)00106 {00107 <span class="comment">// set timer prescaler</span>00108 <a class="code" href="group__timer.html#ga6">timer1SetPrescaler</a>(<a class="code" href="group__timer.html#ga16">TIMER_CLK_DIV1</a>);00109 <span class="comment">// calculate division factor for requested baud rate, and set it</span>00110 UartswBaudRateDiv = (u16)((F_CPU+(baudrate/2L))/(baudrate*1L));00111 }00112 <span class="comment"></span>00113 <span class="comment">//! returns the receive buffer structure </span><a name="l00114"></a><a class="code" href="uartsw2_8h.html#a3">00114</a> <span class="comment"></span><a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="uartsw_8c.html#a13">uartswGetRxBuffer</a>(<span class="keywordtype">void</span>)00115 {00116 <span class="comment">// return rx buffer pointer</span>00117 <span class="keywordflow">return</span> &uartswRxBuffer;00118 }00119 <a name="l00120"></a><a class="code" href="uartsw2_8h.html#a5">00120</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a14">uartswSendByte</a>(u08 data)00121 {00122 <span class="comment">// wait until uart is ready</span>00123 <span class="keywordflow">while</span>(UartswTxBusy);00124 <span class="comment">// set busy flag</span>00125 UartswTxBusy = TRUE;00126 <span class="comment">// save data</span>00127 UartswTxData = data;00128 <span class="comment">// set number of bits (+1 for stop bit)</span>00129 UartswTxBitNum = 9;00130 00131 <span class="comment">// set the start bit</span>00132 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00133 <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>);00134 <span class="preprocessor"> #else</span>00135 <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>);00136 <span class="preprocessor"> #endif</span>00137 <span class="preprocessor"></span>00138 <span class="comment">// schedule the next bit</span>00139 outw(OCR1A, inw(TCNT1) + UartswBaudRateDiv);00140 <span class="comment">// enable OC1A interrupt</span>00141 sbi(TIMSK, OCIE1A);00142 }00143 <span class="comment"></span>00144 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span><a name="l00145"></a><a class="code" href="uartsw2_8h.html#a6">00145</a> <span class="comment"></span>u08 <a class="code" href="uartsw_8c.html#a15">uartswReceiveByte</a>(u08* rxData)00146 {00147 <span class="comment">// make sure we have a receive buffer</span>00148 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o1">size</a>)00149 {00150 <span class="comment">// make sure we have data</span>00151 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)00152 {00153 <span class="comment">// get byte from beginning of buffer</span>00154 *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartswRxBuffer);00155 <span class="keywordflow">return</span> TRUE;00156 }00157 <span class="keywordflow">else</span>00158 {00159 <span class="comment">// no data</span>00160 <span class="keywordflow">return</span> FALSE;00161 }00162 }00163 <span class="keywordflow">else</span>00164 {00165 <span class="comment">// no buffer</span>00166 <span class="keywordflow">return</span> FALSE;00167 }00168 }00169 <a name="l00170"></a><a class="code" href="uartsw2_8h.html#a7">00170</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>(<span class="keywordtype">void</span>)00171 {00172 <span class="keywordflow">if</span>(UartswTxBitNum)00173 {00174 <span class="comment">// there are bits still waiting to be transmitted</span>00175 <span class="keywordflow">if</span>(UartswTxBitNum > 1)00176 {00177 <span class="comment">// transmit data bits (inverted, LSB first)</span>00178 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00179 <span class="preprocessor"></span> <span class="keywordflow">if</span>( !(UartswTxData & 0x01) )00180 <span class="preprocessor"> #else</span>00181 <span class="preprocessor"></span> <span class="keywordflow">if</span>( (UartswTxData & 0x01) )00182 <span class="preprocessor"> #endif</span>00183 <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>);00184 <span class="keywordflow">else</span>00185 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>);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -