【转】在keil中使⽤printf()函数的要点
在⽹上资料时到⼀篇介绍在keil中使⽤printf()函数的⽂章,copy到这⾥,作为备忘。
在keil中printf默认是向串⼝中发送数据的,所以,如果应⽤该函数,必须先初始化串⼝,否则可能引起死机的情况,并且在printf之前应该先将TI置位,摘抄原因如下:
1.printf函数是调⽤putchar函数输⼊的,⽽putchar应该是先判断ti是否为1,不为1则等待为1。如果为1则清0,然后送出⼀ 个字符。因此你如果直接使⽤printf函数,你的程序就会在putchar函数中等待ti为1。这时你的程序就相当于直接死掉了。你可以通过改写 putchar函数实现⾃⼰的⽬的的。
2.Keil的串⼝处理⽐较巧妙的,我的分析如下:
putchar.c⾥⾯,是先检测TI再发送。这样做的⽬的是把尽可能多的时间留给2次串⼝操作之间的程序,⽽不是把等待字节发送的时间⽩⽩空等待浪费掉。所以,在系统初始化的时候,⼀定要令TI=1; 就可以顺畅的使⽤printf函数了。搂主sbuf=" "的办法,其实就是令TI=1.  另外要特别注意,printf函数执⾏完毕后,最后⼀个字节并未发送完毕,例如在485通讯中,此时如果切换为收模式,会丢失最后⼀字节.
3.⼀般串⼝发送都是等TI(字节发送完标志)为1就马上发送下⼀字节,由于不管是中断还是查询TI标志的⽅法,都会检测TI,因此⾸次发送必须置 位TI标志,使串⼝开始发送你的“在程序的初始化部分往串⼝数
据寄存器SBUF⾥放⼀个字符来起⽤终端显⽰;”⽅法最终作⽤也就是把TI 置1,改成 TI=1;来启动发送也是⼀样的(当然,不会发出那个' '字符了)。
4.<stdio.h>中定义,调⽤底层的putchar()来实现.底层发送数据到串⼝时,先查TI=1是否成⽴,死等直到TI=1时将新数据写⼊SBUF,函数返回,所以要先将TI置1,启动第⼀次传输操作.可查看反汇编相关代码理解其⼯作机理!
⽂章转载⾃:
下⾯举⼀个简单的例⼦:
//===========================
#include <reg51.h>
#include <stdio.h>
//-------------------------------
java文本域加滚动条
int main()
{
Uart_init();  //初始化串⼝,这⾥就不写具体代码了。
TI = 1;                        //keil 调⽤stdio.h中printf函数前要置位。
while(1)
{
printf("Hello world!\n");
delay_ms(800);  //延时程序,这⾥也不写具体代码了。
}
return 0;
}
------------------------------
printf
»»» printf
Summary#include <stdio.h>
int printf (
const char *fmtstr      /* format string */
<[>, arguments ... <]>);  /* additional arguments */
The printf function formats a series of strings and numeric values and builds a string to write to the output
stream using theputchar function. The fmtstrargument is a format string that may be composed of
characters, escape sequences, and format specifications.
Ordinary characters and escape sequences are copied to the stream in the order in which they are
interpreted. Format specifications always begin with a percent sign ('%') and require that
additionalarguments are included in the printf function call.
The format string is read from left to right. The first format specification encountered references the
firstargument after fmtstr and converts and outputs it using the format specification. The second format
specification accesses the secondargument after fmtstr, and so on. If there are more arguments than format
specifications, extra arguments are ignored. Results are unpredictable if there are not enough arguments for
the format specifications or if the argument types do not match those specified byfmtstr.
Format specifications have the following general format:
% <[>flags<]> <[>width<]> <[>.precision<]> <[>{b|B|l|L}<]> type
Each field in the format specification may be a single character or a number which specifies a particular
format option.
The type field is a single character that specifies whether the argument is interpreted as a character, string,
number, or pointer, as shown in the following table.
Type Argument
什么是电脑编程
Type
Input Format
d int Signed decimal number.
u unsigned int Unsigned decimal number.
o unsigned int Unsigned octal number.
x unsigned int Unsigned hexadecimal number using "0123456789abcedf". X unsigned int Unsigned hexadecimal number using "0123456789ABCDEF".
f float Floating-point number formatted as <[>-<]>dddd.dddd.
e float Floating-point number formatted as <[>-<]>d.dddd e<[>-<]>dd.
页面设计培训排名
E float Floating-point number formatted as <[>-<]>d.dddd E<[>-<]>dd.
g float Floating-point number using either the e or f format, whichever is more compact for the specified value and precision.
G float Floating-point number using either the E or f format, whichever is more compact for the specified value and precision.
putchar函数c char    A single character.
s*  A string of characters terminated by a null character ('\0').
p*A generic pointer formatted as t:aaaa where t is the memory type and aaaa is the hexadecimal address.
Note
The optional characters l or L may immediately precede the type character to respectively specify long types for d, i, u, o, x, and X.
The optional characters b or B may immediately precede the type character to respectively specify char
Description
parse怎么读
types for d, i, u, o, x, and X.
Characters following a percent sign that are not recognized as a format specification are treated as ordinary
characters. For example, "%%" writes a single percent sign to the output stream.
The flags field is a single character used to justify the output and to print +/- signs and blanks, decimal
points, and octal and hexadecimal prefixes, as shown in the following table.
Flag Description
-Left justify the output in the specified field width.
+Prefix the output value with a + or - sign if the output is a signed type.
blank (' ')Prefix the output value with a blank if it is a signed positive value. Otherwise, no blank is prefixed.
although引导的让步状语从句#Prefixes a non-zero output value with 0, 0x, or 0X when used with o, x, and X field types, respectively.
When used with the e, E, f, g, and G field types, the # flag forces the output value to include a decimal point. The # flag is ignored in all other cases.
The width field is a non-negative number that specifies the minimum number of characters printed. If the number of characters in the output value is less than width, blanks are added on the left (by default) or right (when the - flag is specified) to pad to the minimum width. If width is prefixed with a '0', zeros are padded instead of blanks. The width field never truncates the output. If the length of the output value exceeds the specified width, all characters are output.
The width field may be an asterisk ('*'), in which case an int argument from the argument list provides
the width value. Specifying a 'b' in front of the asterisk specifies that the argument is an unsigned char.
The precision field is a non-negative number that specifies the number of characters to print, the number of significant digits, or the number of decimal places. The precision field can cause truncation or rounding of the output value in the case of a floating-point number as specified in the following table.
Type Precision Field Meaning
d,u,o,x,X The precision field specifies the minimum number of digits that are included in the output value. Digits are not truncated if the number of digits in the argument exceeds that defined in the precision field. If the number of digits in the argument is less than the precision field, the output value is padded on the left with zeros.
f The precision field specifies the number of digits to the right of the decimal point. The last digit is rounded. e,E The precision field specifies the number of digits to the right of the decimal point. The last digit is rounded. g,G The precision field specifies the maximum number of significant digits in the output value.
s The precision field specifies the maximum number of characters in the output value. Excess characters are not output.
c,p The precision field has no effect on these field types.
The precision field may be an asterisk ('*'), in which case an int argument from the argument list provides the
value. Specifying a 'b' in front of the asterisk specifies that the argument is an unsigned char.
Note
You must ensure that the argument type matches that of the format specification. You may use type
casts to ensure that the proper type is passed to printf.
This function is implementation-specific and is based on the operation of
the_getkey and putchar functions. These functions, as provided in the standard library, read and write
characters using the microcontroller's serial port. Custom functions may use other I/O devices.
The total number of bytes that may be passed to this function is limited due to the memory restrictions
imposed by the 8051. A maximum of 15 bytes may be passed in SMALL or COMPACT model. A
maximum of 40 bytes may be passed in LARGE model.
Return
The printf function returns the number of characters actually written to the output stream.
Value
See Also,,,,,,,,,,
Example #include <stdio.h>
void tst_printf (void) {
char a = 1;
int b  = 12365;
long c = 0x7FFFFFFF;
unsigned char x = 'A';
unsigned int y  = 54321;
unsigned long z = 0x4A6F6E00;
float f = 10.0;
float g = 22.95;
char buf [] = "Test String";
char *p = buf;
printf ("char %bd int %d long %ld\n",a,b,c);
printf ("Uchar %bu Uint %u Ulong %lu\n",x,y,z);  printf ("xchar %bx xint %x xlong %lx\n",x,y,z);  printf ("String %s is at address %p\n",buf,p);  printf ("%f != %g\n", f, g);
printf ("%*f != %*g\n", (int)8, f, (int)8, g);
}
--------------------------------
C51: PRINTF GIVES WRONG VALUES
Information in this article applies to:
C51 All Versions
QUESTION
In C51, I am using printf and sprintf with many arguments and it is printing out wrong values. What is the reason for this behavior?
ANSWER
There are any number of reasons why printf can output incorrect values.
Probably the most common reason is that the format specifier and the argument type do not match. To solve this kind of problem, explicitly type cast each argument to the desired type. Be sure that you accurately specify the correct format specifier for that type.
Another reason may be that the number of bytes you may pass to functions with variable-length argument lists is fixed (since arguments are not passed on the stack). In small and compact memory model, printf and sprintf only reserve 15 bytes for the argument list. In the large memory model 40 bytes are reserved. For example, 15 bytes allow you to pass up to five generic (3-byte) pointers, or one generic pointer and three long arguments. This parameter limitation is a compromise to the limited memory available in 8051 devices.
Eight arguments will not fit in the space reserved for small or compact memory models. So, instead of one sprintf call with many arguments you must perform two or three calls and distribute the arguments among the sprintf calls. For example:
The following code exceeds the parameter space limits since seven generic pointers (21 bytes) are passed to sprintf.
char str[] = "Any Text";
sprintf (buff_out, "%s %s %s %s %s", str, str, str, str, str);
The above example may be rewritten as follows:
char str[] = "Any Text";
int len;
len = sprintf (buff_out, "%s %s %s ", str, str, str);
sprintf (buff_out + len, "%s %s", str, str);
MORE INFORMATION
Refer to  in the Cx51 User's Guide.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。