📄 uartsw2_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: uartsw2.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>uartsw2.c</h1><a href="uartsw2_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uartsw2.c \brief Software Interrupt-driven UART Driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'uartsw2.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.6</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="uartsw2_8h.html">uartsw2.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> u08 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_8c.html#a9">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 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00057 <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>);00058 <span class="preprocessor"> #else</span>00059 <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>);00060 <span class="preprocessor"> #endif</span>00061 <span class="preprocessor"></span> 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>);00062 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>);00063 <span class="comment">// initialize baud rate</span>00064 <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(9600);00065 00066 <span class="comment">// setup the transmitter</span>00067 UartswTxBusy = FALSE;00068 <span class="comment">// disable OC2 interrupt</span>00069 cbi(TIMSK, OCIE2);00070 <span class="comment">// attach TxBit service routine to OC2</span>00071 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER2OUTCOMPARE_INT, <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>);00072 00073 <span class="comment">// setup the receiver</span>00074 UartswRxBusy = FALSE;00075 <span class="comment">// disable OC0 interrupt</span>00076 cbi(TIMSK, OCIE0);00077 <span class="comment">// attach RxBit service routine to OC0</span>00078 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER0OUTCOMPARE_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>);00079 <span class="comment">// INT2 trigger on rising/falling edge</span>00080 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>00081 <span class="preprocessor"></span> sbi(MCUCSR, ISC2); <span class="comment">// rising edge</span>00082 <span class="preprocessor"> #else</span>00083 <span class="preprocessor"></span> cbi(MCUCSR, ISC2); <span class="comment">// falling edge</span>00084 <span class="preprocessor"> #endif</span>00085 <span class="preprocessor"></span> <span class="comment">// enable INT2 interrupt</span>00086 sbi(GICR, INT2);00087 00088 <span class="comment">// turn on interrupts</span>00089 sei();00090 }00091 <span class="comment"></span>00092 <span class="comment">//! create and initialize the uart buffers</span><a name="l00093"></a><a class="code" href="uartsw2_8c.html#a10">00093</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>)00094 {00095 <span class="comment">// initialize the UART receive buffer</span>00096 <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>);00097 }00098 <span class="comment"></span>00099 <span class="comment">//! turns off software UART</span><a name="l00100"></a><a class="code" href="uartsw2_8c.html#a11">00100</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>)00101 {00102 <span class="comment">// disable interrupts</span>00103 cbi(TIMSK, OCIE2);00104 cbi(TIMSK, OCIE0);00105 cbi(GICR, INT2);00106 <span class="comment">// detach the service routines</span>00107 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER2OUTCOMPARE_INT);00108 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER0OUTCOMPARE_INT);00109 }00110 <a name="l00111"></a><a class="code" href="uartsw2_8c.html#a12">00111</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(u32 baudrate)00112 {00113 u16 div;00114 00115 <span class="comment">// set timer prescaler</span>00116 <span class="keywordflow">if</span>( baudrate > (F_CPU/64L*256L) )00117 {00118 <span class="comment">// if the requested baud rate is high,</span>00119 <span class="comment">// set timer prescalers to div-by-64</span>00120 <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga28">TIMERRTC_CLK_DIV64</a>);00121 <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga18">TIMER_CLK_DIV64</a>);00122 div = 64;00123 }00124 <span class="keywordflow">else</span>00125 {00126 <span class="comment">// if the requested baud rate is low,</span>00127 <span class="comment">// set timer prescalers to div-by-256</span>00128 <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga30">TIMERRTC_CLK_DIV256</a>);00129 <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga19">TIMER_CLK_DIV256</a>);00130 div = 256;00131 }00132 00133 <span class="comment">// calculate division factor for requested baud rate, and set it</span>00134 <span class="comment">//UartswBaudRateDiv = (u08)(((F_CPU/64L)+(baudrate/2L))/(baudrate*1L));</span>00135 <span class="comment">//UartswBaudRateDiv = (u08)(((F_CPU/256L)+(baudrate/2L))/(baudrate*1L));</span>00136 UartswBaudRateDiv = (u08)(((F_CPU/div)+(baudrate/2L))/(baudrate*1L));00137 }00138 <span class="comment"></span>00139 <span class="comment">//! returns the receive buffer structure </span><a name="l00140"></a><a class="code" href="uartsw2_8c.html#a13">00140</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>)00141 {00142 <span class="comment">// return rx buffer pointer</span>00143 <span class="keywordflow">return</span> &uartswRxBuffer;00144 }00145 <a name="l00146"></a><a class="code" href="uartsw2_8c.html#a14">00146</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a14">uartswSendByte</a>(u08 data)00147 {00148 <span class="comment">// wait until uart is ready</span>00149 <span class="keywordflow">while</span>(UartswTxBusy);00150 <span class="comment">// set busy flag</span>00151 UartswTxBusy = TRUE;00152 <span class="comment">// save data</span>00153 UartswTxData = data;00154 <span class="comment">// set number of bits (+1 for stop bit)</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -