⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rprintf_8c-source.html

📁 avr应用测试程序
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!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.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div><h1>rprintf.c</h1><a href="rprintf_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">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 &lt;avr/pgmspace.h&gt;</span>00023 <span class="comment">//#include &lt;string-avr.h&gt;</span>00024 <span class="comment">//#include &lt;stdlib.h&gt;</span>00025 <span class="preprocessor">#include &lt;stdarg.h&gt;</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)?(pgm_read_byte(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[] = "0123456789ABCDEF";00046 00047 #define hexchar(x)  pgm_read_byte( HexChars+((x)&amp;0x0f) )00048 <span class="comment">//#define hexchar(x)    ((((x)&amp;0x0F)&gt;9)?((x)+'A'-10):((x)+'0'))</span>00049 00050 <span class="comment">// function pointer to single character output routine</span>00051 static <span class="keywordtype">void</span> (*rputchar)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c);00052 00053 <span class="comment">// *** rprintf initialization ***</span>00054 <span class="comment">// you must call this function once and supply the character output</span>00055 <span class="comment">// routine before using other functions in this library</span>00056 <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga0">rprintfInit</a>(<span class="keywordtype">void</span> (*putchar_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c))00057 {00058     rputchar = putchar_func;00059 }00060 00061 <span class="comment">// *** rprintfChar ***</span>00062 <span class="comment">// send a character/byte to the current output device</span><a name="l00063"></a><a class="code" href="group__rprintf.html#ga1">00063</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c)00064 {00065     <span class="comment">// do LF -&gt; CR/LF translation</span>00066     <span class="keywordflow">if</span>(c == <span class="charliteral">'\n'</span>)00067         rputchar(<span class="charliteral">'\r'</span>);00068     <span class="comment">// send character</span>00069     rputchar(c);00070 }00071 00072 <span class="comment">// *** rprintfStr ***</span>00073 <span class="comment">// prints a null-terminated string stored in RAM</span><a name="l00074"></a><a class="code" href="group__rprintf.html#ga2">00074</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga2">rprintfStr</a>(<span class="keywordtype">char</span> str[])00075 {00076     <span class="comment">// send a string stored in RAM</span>00077     <span class="comment">// check to make sure we have a good pointer</span>00078     <span class="keywordflow">if</span> (!str) <span class="keywordflow">return</span>;00079 00080     <span class="comment">// print the string until a null-terminator</span>00081     <span class="keywordflow">while</span> (*str)00082         <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(*str++);00083 }00084 00085 <span class="comment">// *** rprintfStrLen ***</span>00086 <span class="comment">// prints a section of a string stored in RAM</span>00087 <span class="comment">// begins printing at position indicated by &lt;start&gt;</span>00088 <span class="comment">// prints number of characters indicated by &lt;len&gt;</span><a name="l00089"></a><a class="code" href="group__rprintf.html#ga3">00089</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga3">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)00090 {00091     <span class="keyword">register</span> <span class="keywordtype">int</span> i=0;00092 00093     <span class="comment">// check to make sure we have a good pointer</span>00094     <span class="keywordflow">if</span> (!str) <span class="keywordflow">return</span>;00095     <span class="comment">// spin through characters up to requested start</span>00096     <span class="comment">// keep going as long as there's no null</span>00097     <span class="keywordflow">while</span>((i++&lt;start) &amp;&amp; (*str++));00098 <span class="comment">//  for(i=0; i&lt;start; i++)</span>00099 <span class="comment">//  {</span>00100 <span class="comment">//      // keep steping through string as long as there's no null</span>00101 <span class="comment">//      if(*str) str++;</span>00102 <span class="comment">//  }</span>00103 00104     <span class="comment">// then print exactly len characters</span>00105     <span class="keywordflow">for</span>(i=0; i&lt;len; i++)00106     {00107         <span class="comment">// print data out of the string as long as we haven't reached a null yet</span>00108         <span class="comment">// at the null, start printing spaces</span>00109         <span class="keywordflow">if</span>(*str)00110             <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(*str++);00111         <span class="keywordflow">else</span>00112             <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(<span class="charliteral">' '</span>);00113     }00114 00115 }00116 00117 <span class="comment">// *** rprintfProgStr ***</span>00118 <span class="comment">// prints a null-terminated string stored in program ROM</span><a name="l00119"></a><a class="code" href="group__rprintf.html#ga4">00119</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga4">rprintfProgStr</a>(<span class="keyword">const</span> prog_char str[])00120 {00121     <span class="comment">// print a string stored in program memory</span>00122     <span class="keyword">register</span> <span class="keywordtype">char</span> c;00123 00124     <span class="comment">// check to make sure we have a good pointer</span>00125     <span class="keywordflow">if</span> (!str) <span class="keywordflow">return</span>;00126     00127     <span class="comment">// print the string until the null-terminator</span>00128     <span class="keywordflow">while</span>((c = pgm_read_byte(str++)))00129         <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(c);00130 }00131 00132 <span class="comment">// *** rprintfCRLF ***</span>00133 <span class="comment">// prints carriage return and line feed</span><a name="l00134"></a><a class="code" href="group__rprintf.html#ga5">00134</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(<span class="keywordtype">void</span>)00135 {00136     <span class="comment">// print CR/LF</span>00137     <span class="comment">//rprintfChar('\r');</span>00138     <span class="comment">// LF -&gt; CR/LF translation built-in to rprintfChar()</span>00139     <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(<span class="charliteral">'\n'</span>);00140 }00141 00142 <span class="comment">// *** rprintfu04 ***</span>00143 <span class="comment">// prints an unsigned 4-bit number in hex (1 digit)</span><a name="l00144"></a><a class="code" href="group__rprintf.html#ga6">00144</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga6">rprintfu04</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00145 {00146     <span class="comment">// print 4-bit hex value</span>00147 <span class="comment">//  char Character = data&amp;0x0f;</span>00148 <span class="comment">//  if (Character&gt;9)</span>00149 <span class="comment">//      Character+='A'-10;</span>00150 <span class="comment">//  else</span>00151 <span class="comment">//      Character+='0';</span>00152     <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(hexchar(data));00153 }00154 00155 <span class="comment">// *** rprintfu08 ***</span>00156 <span class="comment">// prints an unsigned 8-bit number in hex (2 digits)</span><a name="l00157"></a><a class="code" href="group__rprintf.html#ga7">00157</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00158 {00159     <span class="comment">// print 8-bit hex value</span>00160     <a class="code" href="group__rprintf.html#ga6">rprintfu04</a>(data&gt;&gt;4);00161     <a class="code" href="group__rprintf.html#ga6">rprintfu04</a>(data);00162 }00163 00164 <span class="comment">// *** rprintfu16 ***</span>00165 <span class="comment">// prints an unsigned 16-bit number in hex (4 digits)</span><a name="l00166"></a><a class="code" href="group__rprintf.html#ga8">00166</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga8">rprintfu16</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> data)00167 {00168     <span class="comment">// print 16-bit hex value</span>00169     <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(data&gt;&gt;8);00170     <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(data);00171 }00172 00173 <span class="comment">// *** rprintfu32 ***</span>00174 <span class="comment">// prints an unsigned 32-bit number in hex (8 digits)</span><a name="l00175"></a><a class="code" href="group__rprintf.html#ga9">00175</a> <span class="keywordtype">void</span> <a class="code" href="group__rprintf.html#ga9">rprintfu32</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> data)00176 {00177     <span class="comment">// print 32-bit hex value</span>00178     <a class="code" href="group__rprintf.html#ga8">rprintfu16</a>(data&gt;&gt;16);00179     <a class="code" href="group__rprintf.html#ga8">rprintfu16</a>(data);00180 }00181 00182 <span class="comment">// *** rprintfNum ***</span>00183 <span class="comment">// special printf for numbers only</span>00184 <span class="comment">// see formatting information below</span>00185 <span class="comment">//  Print the number "n" in the given "base"</span>00186 <span class="comment">//  using exactly "numDigits"</span>00187 <span class="comment">//  print +/- if signed flag "isSigned" is TRUE</span>00188 <span class="comment">//  use the character specified in "padchar" to pad extra characters</span>00189 <span class="comment">//</span>00190 <span class="comment">//  Examples:</span>00191 <span class="comment">//  uartPrintfNum(10, 6,  TRUE, ' ',   1234);  --&gt;  " +1234"</span>00192 <span class="comment">//  uartPrintfNum(10, 6, FALSE, '0',   1234);  --&gt;  "001234"</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -