📄 rprintf_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: rprintf.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>rprintf.c</h1><a href="rprintf_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file rprintf.c \brief printf routine and associated routines. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'rprintf.c'</span>00005 <span class="comment">// Title : printf routine and associated routines</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2002</span>00007 <span class="comment">// Created : 2000.12.26</span>00008 <span class="comment">// Revised : 2003.5.1</span>00009 <span class="comment">// Version : 1.0</span>00010 <span class="comment">// Target MCU : Atmel AVR series and other targets</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>00016 <span class="comment">//</span>00017 <span class="comment">// This code is distributed under the GNU Public License</span>00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00019 <span class="comment">//</span>00020 <span class="comment">//*****************************************************************************</span>00021 00022 <span class="comment">//#include <avr/pgmspace.h></span>00023 <span class="comment">//#include <string-avr.h></span>00024 <span class="comment">//#include <stdlib.h></span>00025 <span class="preprocessor">#include <stdarg.h></span>00026 <span class="comment">//#include "global.h"</span>00027 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00028 00029 <span class="preprocessor">#ifndef TRUE</span>00030 <span class="preprocessor"></span><span class="preprocessor"> #define TRUE -1</span>00031 <span class="preprocessor"></span><span class="preprocessor"> #define FALSE 0</span>00032 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00033 <span class="preprocessor"></span>00034 <span class="preprocessor">#define INF 32766 // maximum field size to print</span>00035 <span class="preprocessor"></span><span class="preprocessor">#define READMEMBYTE(a,char_ptr) ((a)?(*char_ptr):(*char_ptr))</span>00036 <span class="preprocessor"></span>00037 <span class="preprocessor">#ifdef RPRINTF_COMPLEX</span>00038 <span class="preprocessor"></span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> buf[128];00039 <span class="preprocessor">#endif</span>00040 <span class="preprocessor"></span>00041 <span class="comment">// use this to store hex conversion in RAM</span>00042 <span class="comment">//static char HexChars[] = "0123456789ABCDEF";</span>00043 <span class="comment">// use this to store hex conversion in program memory</span>00044 <span class="comment">//static prog_char HexChars[] = "0123456789ABCDEF";</span>00045 <span class="keyword">static</span> <span class="keywordtype">char</span> HexChars[] = <span class="stringliteral">"0123456789ABCDEF"</span>;00046 00047 <span class="comment">// function pointer to single character output routine</span>00048 <span class="keyword">static</span> void (*rputchar)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c);00049 00050 <span class="comment">// *** rprintf initialization ***</span>00051 <span class="comment">// you must call this function once and supply the character output</span>00052 <span class="comment">// routine before using other functions in this library</span><a name="l00053"></a><a class="code" href="rprintf_8h.html#a6">00053</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(<span class="keywordtype">void</span> (*putchar_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c))00054 {00055 rputchar = putchar_func;00056 }00057 00058 <span class="comment">// *** rprintfChar ***</span>00059 <span class="comment">// send a character/byte to the current output device</span><a name="l00060"></a><a class="code" href="rprintf_8h.html#a7">00060</a> <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c)00061 {00062 <span class="comment">// send character</span>00063 rputchar(c);00064 }00065 00066 <span class="comment">// *** rprintfStr ***</span>00067 <span class="comment">// prints a null-terminated string stored in RAM</span><a name="l00068"></a><a class="code" href="rprintf_8h.html#a8">00068</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="keywordtype">char</span> str[])00069 {00070 <span class="comment">// send a string stored in RAM</span>00071 <span class="comment">// check to make sure we have a good pointer</span>00072 <span class="keywordflow">if</span> (!str) <span class="keywordflow">return</span>;00073 00074 <span class="comment">// print the string until a null-terminator</span>00075 <span class="keywordflow">while</span> (*str)00076 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(*str++);00077 }00078 00079 <span class="comment">// *** rprintfStrLen ***</span>00080 <span class="comment">// prints a section of a string stored in RAM</span>00081 <span class="comment">// begins printing at position indicated by <start></span>00082 <span class="comment">// prints number of characters indicated by <len></span><a name="l00083"></a><a class="code" href="rprintf_8h.html#a9">00083</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a9">rprintfStrLen</a>(<span class="keywordtype">char</span> str[], <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> start, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> len)00084 {00085 <span class="keyword">register</span> <span class="keywordtype">char</span> i=0;00086 00087 <span class="comment">// check to make sure we have a good pointer</span>00088 <span class="keywordflow">if</span> (!str) <span class="keywordflow">return</span>;00089 <span class="comment">// spin through characters up to requested start</span>00090 <span class="comment">// keep going as long as there's no null</span>00091 <span class="keywordflow">while</span>((i++<start) && (*str++));00092 <span class="comment">// for(i=0; i<start; i++)</span>00093 <span class="comment">// {</span>00094 <span class="comment">// // keep steping through string as long as there's no null</span>00095 <span class="comment">// if(*str) str++;</span>00096 <span class="comment">// }</span>00097 00098 <span class="comment">// then print exactly len characters</span>00099 <span class="keywordflow">for</span>(i=0; i<len; i++)00100 {00101 <span class="comment">// print data out of the string as long as we haven't reached a null yet</span>00102 <span class="comment">// at the null, start printing spaces</span>00103 <span class="keywordflow">if</span>(*str)00104 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(*str++);00105 <span class="keywordflow">else</span>00106 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(<span class="charliteral">' '</span>);00107 }00108 00109 }00110 00111 <span class="comment">// *** rprintfCRLF ***</span>00112 <span class="comment">// prints carriage return and line feed</span><a name="l00113"></a><a class="code" href="rprintf_8h.html#a11">00113</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8c.html#a10">rprintfCRLF</a>(<span class="keywordtype">void</span>)00114 {00115 <span class="comment">// print CR/LF</span>00116 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(<span class="charliteral">'\r'</span>);00117 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(<span class="charliteral">'\n'</span>);00118 }00119 00120 <span class="comment">// *** rprintfu04 ***</span>00121 <span class="comment">// prints an unsigned 4-bit number in hex (1 digit)</span><a name="l00122"></a><a class="code" href="rprintf_8h.html#a12">00122</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a12">rprintfu04</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00123 {00124 <span class="comment">// print 4-bit hex value</span>00125 <span class="comment">// char Character = data&0x0f;</span>00126 <span class="comment">// if (Character>9)</span>00127 <span class="comment">// Character+='A'-10;</span>00128 <span class="comment">// else</span>00129 <span class="comment">// Character+='0';</span>00130 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(HexChars[data&0x0f]);00131 }00132 00133 <span class="comment">// *** rprintfu08 ***</span>00134 <span class="comment">// prints an unsigned 8-bit number in hex (2 digits)</span><a name="l00135"></a><a class="code" href="rprintf_8h.html#a13">00135</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00136 {00137 <span class="comment">// print 8-bit hex value</span>00138 <a class="code" href="rprintf_8h.html#a12">rprintfu04</a>(data>>4);00139 <a class="code" href="rprintf_8h.html#a12">rprintfu04</a>(data);00140 }00141 00142 <span class="comment">// *** rprintfu16 ***</span>00143 <span class="comment">// prints an unsigned 16-bit number in hex (4 digits)</span><a name="l00144"></a><a class="code" href="rprintf_8h.html#a14">00144</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> data)00145 {00146 <span class="comment">// print 16-bit hex value</span>00147 <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(data>>8);00148 <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(data);00149 }00150 00151 <span class="comment">// *** rprintfu32 ***</span>00152 <span class="comment">// prints an unsigned 32-bit number in hex (8 digits)</span><a name="l00153"></a><a class="code" href="rprintf_8h.html#a15">00153</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a15">rprintfu32</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> data)00154 {00155 <span class="comment">// print 32-bit hex value</span>00156 <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(data>>16);00157 <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(data);00158 }00159 00160 <span class="comment">// *** rprintfNum ***</span>00161 <span class="comment">// special printf for numbers only</span>00162 <span class="comment">// see formatting information below</span>00163 <span class="comment">// Print the number "n" in the given "base"</span>00164 <span class="comment">// using exactly "numDigits"</span>00165 <span class="comment">// print +/- if signed flag "isSigned" is TRUE</span>00166 <span class="comment">// use the character specified in "padchar" to pad extra characters</span>00167 <span class="comment">//</span>00168 <span class="comment">// Examples:</span>00169 <span class="comment">// uartPrintfNum(10, 6, TRUE, ' ', 1234); --> " +1234"</span>00170 <span class="comment">// uartPrintfNum(10, 6, FALSE, '0', 1234); --> "001234"</span>00171 <span class="comment">// uartPrintfNum(16, 6, FALSE, '.', 0x5AA5); --> "..5AA5"</span><a name="l00172"></a><a class="code" href="rprintf_8h.html#a16">00172</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a16">rprintfNum</a>(<span class="keywordtype">char</span> base, <span class="keywordtype">char</span> numDigits, <span class="keywordtype">char</span> isSigned, <span class="keywordtype">char</span> padchar, <span class="keywordtype">long</span> n)00173 {00174 <span class="comment">// define a global HexChars or use line below</span>00175 <span class="comment">//static char HexChars[16] = "0123456789ABCDEF";</span>00176 <span class="keywordtype">char</span> *p, buf[32];00177 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> x;00178 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> count;00179 00180 <span class="comment">// prepare negative number</span>00181 <span class="keywordflow">if</span>( isSigned && (n < 0) )00182 {00183 x = -n;00184 }00185 <span class="keywordflow">else</span>00186 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -