📄 uart_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 ARMlib-LPC2100: uart.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>uart.c</h1><a href="uart_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file uart.c \brief UART driver for ARM LPC210x 16550. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'uart.c'</span>00005 <span class="comment">// Title : UART driver for ARM LPC210x 16550</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2004</span>00007 <span class="comment">// Created : 4/2/2004</span>00008 <span class="comment">// Revised : 4/2/2004</span>00009 <span class="comment">// Version : 0.1</span>00010 <span class="comment">// Target MCU : Philips ARM LPC210x Series</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 <limits.h></span>00019 <span class="preprocessor">#include "LPC210x.h"</span>00020 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="comment"></span>00024 <span class="comment">//! enable and initialize the uart</span><a name="l00025"></a><a class="code" href="uart_8h.html#a18">00025</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uart_8h.html#a18">uart0Init</a>(uint16_t baud, uint8_t mode, uint8_t fifomode)00026 {00027 <span class="comment">// set port pins for UART0</span>00028 PINSEL0 = (PINSEL0 & ~U0_PINMASK) | U0_PINSEL;00029 00030 U0IER = 0x00; <span class="comment">// disable all interrupts</span>00031 U0IIR; <span class="comment">// clear interrupt ID</span>00032 U0RBR; <span class="comment">// clear receive register</span>00033 U0LSR; <span class="comment">// clear line status register</span>00034 00035 <span class="comment">// set the baudrate</span>00036 U0LCR = ULCR_DLAB_ENABLE; <span class="comment">// select divisor latches </span>00037 U0DLL = (uint8_t)baud; <span class="comment">// set for baud low byte</span>00038 U0DLM = (uint8_t)(baud >> 8); <span class="comment">// set for baud high byte</span>00039 00040 <span class="comment">// set the number of characters and other</span>00041 <span class="comment">// user specified operating parameters</span>00042 U0LCR = (mode & ~ULCR_DLAB_ENABLE);00043 U0FCR = fifomode;00044 }00045 <a name="l00046"></a><a class="code" href="uart_8h.html#a20">00046</a> <span class="keywordtype">int</span> <a class="code" href="uart_8h.html#a20">uart0SendByte</a>(<span class="keywordtype">int</span> data)00047 {00048 <span class="keywordflow">while</span>(!(U0LSR & ULSR_THRE)) <span class="comment">// wait for TX buffer to empty</span>00049 <span class="keywordflow">continue</span>; <span class="comment">// also either WDOG() or swap()</span>00050 00051 U0THR = (uint8_t)data;00052 <span class="keywordflow">return</span> (uint8_t)data;00053 }00054 <a name="l00055"></a><a class="code" href="uart_8h.html#a22">00055</a> <span class="keywordtype">int</span> <a class="code" href="uart_8c.html#a2">uart0GetByte</a>(<span class="keywordtype">void</span>)00056 {00057 <span class="keywordflow">if</span>(U0LSR & ULSR_RDR) <span class="comment">// check if character is available</span>00058 <span class="keywordflow">return</span> U0RBR; <span class="comment">// return character</span>00059 <span class="keywordflow">return</span> -1;00060 }00061 00062 <span class="keywordtype">void</span> uart1Init(uint16_t baud, uint8_t mode, uint8_t fifomode)00063 {00064 <span class="comment">// set port pins for UART1</span>00065 PINSEL0 = (PINSEL0 & ~U1_PINMASK) | U1_PINSEL;00066 00067 U1IER = 0x00; <span class="comment">// disable all interrupts</span>00068 U1IIR; <span class="comment">// clear interrupt ID</span>00069 U1RBR; <span class="comment">// clear receive register</span>00070 U1LSR; <span class="comment">// clear line status register</span>00071 00072 <span class="comment">// set the baudrate</span>00073 U1LCR = ULCR_DLAB_ENABLE; <span class="comment">// select divisor latches </span>00074 U1DLL = (uint8_t)baud; <span class="comment">// set for baud low byte</span>00075 U1DLM = (uint8_t)(baud >> 8); <span class="comment">// set for baud high byte</span>00076 00077 <span class="comment">// set the number of characters and other</span>00078 <span class="comment">// user specified operating parameters</span>00079 U1LCR = (mode & ~ULCR_DLAB_ENABLE);00080 U1FCR = fifomode;00081 }00082 00083 <span class="keywordtype">int</span> uart1SendByte(<span class="keywordtype">int</span> data)00084 {00085 <span class="keywordflow">while</span>(!(U1LSR & ULSR_THRE)) <span class="comment">// wait for TX buffer to empty</span>00086 <span class="keywordflow">continue</span>; <span class="comment">// also either WDOG() or swap()</span>00087 U1THR = (uint8_t)data;00088 <span class="keywordflow">return</span> (uint8_t)data;00089 }00090 00091 <span class="keywordtype">int</span> uart1GetByte(<span class="keywordtype">void</span>)00092 { 00093 <span class="keywordflow">if</span>(U1LSR & ULSR_RDR) <span class="comment">// check if character is available</span>00094 <span class="keywordflow">return</span> U1RBR; <span class="comment">// return character</span>00095 <span class="keywordflow">return</span> -1;00096 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 13 03:38:12 2004 for Procyon ARMlib-LPC2100 by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -