📄 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 AVRlib: 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="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</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>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="preprocessor">#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="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</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)?(PRG_RDB(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> __attribute__ ((progmem)) 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>00053 <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">int</span> start, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len)00084 {00085 <span class="keyword">register</span> <span class="keywordtype">int</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">// *** rprintfProgStr ***</span>00112 <span class="comment">// prints a null-terminated string stored in program ROM</span><a name="l00113"></a><a class="code" href="rprintf_8h.html#a10">00113</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8h.html#a10">rprintfProgStr</a>(<span class="keyword">const</span> prog_char str[])00114 {00115 <span class="comment">// print a string stored in program memory</span>00116 <span class="keyword">register</span> <span class="keywordtype">char</span> c;00117 00118 <span class="comment">// check to make sure we have a good pointer</span>00119 <span class="keywordflow">if</span> (!str) <span class="keywordflow">return</span>;00120 00121 <span class="comment">// print the string until the null-terminator</span>00122 <span class="keywordflow">while</span>((c = PRG_RDB(str++)))00123 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(c);00124 }00125 00126 <span class="comment">// *** rprintfCRLF ***</span>00127 <span class="comment">// prints carriage return and line feed</span><a name="l00128"></a><a class="code" href="rprintf_8h.html#a11">00128</a> <span class="keywordtype">void</span> <a class="code" href="rprintf_8c.html#a9">rprintfCRLF</a>(<span class="keywordtype">void</span>)00129 {00130 <span class="comment">// print CR/LF</span>00131 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(<span class="charliteral">'\r'</span>);00132 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(<span class="charliteral">'\n'</span>);00133 }00134 00135 <span class="comment">// *** rprintfu04 ***</span>00136 <span class="comment">// prints an unsigned 4-bit number in hex (1 digit)</span><a name="l00137"></a><a class="code" href="rprintf_8h.html#a12">00137</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)00138 {00139 <span class="comment">// print 4-bit hex value</span>00140 <span class="comment">// char Character = data&0x0f;</span>00141 <span class="comment">// if (Character>9)</span>00142 <span class="comment">// Character+='A'-10;</span>00143 <span class="comment">// else</span>00144 <span class="comment">// Character+='0';</span>00145 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(PRG_RDB( HexChars+(data&0x0f) ));00146 }00147 00148 <span class="comment">// *** rprintfu08 ***</span>00149 <span class="comment">// prints an unsigned 8-bit number in hex (2 digits)</span><a name="l00150"></a><a class="code" href="rprintf_8h.html#a13">00150</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)00151 {00152 <span class="comment">// print 8-bit hex value</span>00153 <a class="code" href="rprintf_8h.html#a12">rprintfu04</a>(data>>4);00154 <a class="code" href="rprintf_8h.html#a12">rprintfu04</a>(data);00155 }00156 00157 <span class="comment">// *** rprintfu16 ***</span>00158 <span class="comment">// prints an unsigned 16-bit number in hex (4 digits)</span><a name="l00159"></a><a class="code" href="rprintf_8h.html#a14">00159</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)00160 {00161 <span class="comment">// print 16-bit hex value</span>00162 <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(data>>8);00163 <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(data);00164 }00165 00166 <span class="comment">// *** rprintfu32 ***</span>00167 <span class="comment">// prints an unsigned 32-bit number in hex (8 digits)</span><a name="l00168"></a><a class="code" href="rprintf_8h.html#a15">00168</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)00169 {00170 <span class="comment">// print 32-bit hex value</span>00171 <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(data>>16);00172 <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(data);00173 }00174 00175 <span class="comment">// *** rprintfNum ***</span>00176 <span class="comment">// special printf for numbers only</span>00177 <span class="comment">// see formatting information below</span>00178 <span class="comment">// Print the number "n" in the given "base"</span>00179 <span class="comment">// using exactly "numDigits"</span>00180 <span class="comment">// print +/- if signed flag "isSigned" is TRUE</span>00181 <span class="comment">// use the character specified in "padchar" to pad extra characters</span>00182 <span class="comment">//</span>00183 <span class="comment">// Examples:</span>00184 <span class="comment">// uartPrintfNum(10, 6, TRUE, ' ', 1234); --> " +1234"</span>00185 <span class="comment">// uartPrintfNum(10, 6, FALSE, '0', 1234); --> "001234"</span>00186 <span class="comment">// uartPrintfNum(16, 6, FALSE, '.', 0x5AA5); --> "..5AA5"</span><a name="l00187"></a><a class="code" href="rprintf_8h.html#a16">00187</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)00188 {00189 <span class="comment">// define a global HexChars or use line below</span>00190 <span class="comment">//static char HexChars[16] = "0123456789ABCDEF";</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -