当前位置:首页 > 单片机 > 单片机
[导读]在Keil C51 中使用printf ,首先需要重新实现 putchar(char c)函数。此函数在charputchar(charc){ES=0;SBUF=c;while(TI==0);TI=0;ES=1;return0;}我们先分析一下上面这个程序哈,关闭串口中断发送单字节数据等待发送完

在Keil C51 中使用printf ,首先需要重新实现 putchar(char c)函数。此函数在

charputchar(charc){ES=0;SBUF=c;while(TI==0);TI=0;ES=1;return0;}

我们先分析一下上面这个程序哈,
关闭串口中断
发送单字节数据
等待发送完毕
清除TI标志
开启串口中断

在main函数里可以直接使用printf函数进行输出了。
但是,我一直存在这样一个疑惑:

voidmain(){unsignedchartest1=55;printf("thetestis%drn",test1);}


使用串口输出的数值一直不对,我后来自己理解,%d是整型,而在Keil C51整型占用2个byte,所以我一般的解决办法是做一次强制类型转换:

voidmain(){unsignedchartest1=55;printf("thetestis%drn",(int)test1);}


后来阅读Keil C51的帮助手册:
得到这样一条信息:

格式含义针对类型%d两个字节变量int%bd单字节变量char%ld四字节变量long int

所以上面的问题的另一个解决方案是:

voidmain(){unsignedchartest1=55;printf("thetestis%bdrn",test1);}


下面附上Keil C51手册内容。
int printf (

const charfmtstr /format string */
<[>, arguments … <]>); /* additional arguments */

Description The printf function formats a series of strings and numeric values and builds a string to write to the output stream using the putchar function. The fmtstr argument 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 additional arguments are included in the printf function call.

The format string is read from left to right. The first format specification encountered references the first argument after fmtstr and converts and outputs it using the format specification. The second format specification accesses the second argument 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 by fmtstr.

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 ArgumentType InputFormatdintSigned decimal number.uunsigned intUnsigned decimal number.ounsigned intUnsigned octal number.xunsigned intUnsigned hexadecimal number using “0123456789abcedf”.Xunsigned intUnsigned hexadecimal number using “0123456789ABCDEF”.ffloatFloating-point number formatted as<[>-<]>dddd.dddd.efloatFloating-point number formatted as<[>-<]>d.dddde<[>-<]>dd.EfloatFloating-point number formatted as<[>-<]>d.ddddE<[>-<]>dd.gfloatFloating-point number using either the e or f format, whichever is more compact for the specified value and precision.GfloatFloating-point number using either the E or f format, whichever is more compact for the specified value and precision.ccharA single character.s*A string of characters terminated by a null character (‘’).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 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.

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 Value The printf function returns the number of characters actually written to the output stream.

See Also gets, printf517, puts, scanf, scanf517, sprintf, sprintf517, sscanf, sscanf517, vprintf, vsprintf

Example#includevoidtst_printf(void){chara=1;intb=12365;longc=0x7FFFFFFF;unsignedcharx='A';unsignedinty=54321;unsignedlongz=0x4A6F6E00;floatf=10.0;floatg=22.95;charbuf[]="TestString";char*p=buf;printf("char%bdint%dlong%ldn",a,b,c);printf("Uchar%buUint%uUlong%lun",x,y,z);printf("xchar%bxxint%xxlong%lxn",x,y,z);printf("String%sisataddress%pn",buf,p);printf("%f!=%gn",f,g);printf("%*f!=%*gn",(int)8,f,(int)8,g);}



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

嵌入式开发作为一种专业且技术密集型的领域,涵盖了从硬件底层驱动、中间件到应用层软件开发等多个层面的工作,其所需的工具种类繁多,各有针对性,旨在提升开发效率、保证代码质量以及简化调试过程。

关键字: 嵌入式开发 keil

在实际项目中,我们经常需要提取一个数值的某些位的数码,比如用数码管来显示数值或将一个数值转成字符串,都会涉及到这一操作。

关键字: 数值 数码 printf

单片机内部有很多的特殊功能寄存器,每个寄存器在单片机内部都分配有唯一的地址,一般我们会根据寄存器功能的不同给寄存器赋予各自的名称,当我们需要在程序中操作这些特殊功能寄存器时,必须要在程序的最前面将这些名称加以声明,声明的...

关键字: C51 数据类型 扩充定义

数据元(Data Element),也称为数据元素,是用一组属性描述其定义、标识、表示和允许值的数据单元,在一定语境下,通常用于构建一个语义正确、独立且无歧义的特定概念语义的信息单元。数据元可以理解为数据的基本单元,将若...

关键字: C51 数据类型

之后新建新的工程,添加.a文件就可以使用了,当然也可以使用keil来添加,但是keil默认的是用.lab,需要自己配置一下文件属性,改为lib文件即可。一半release sdk的时候用这种方式很关键的,毕竟自己的核心代...

关键字: keil 文件属性 lib文件

▼点击下方名片,关注公众号▼欢迎关注【玩转单片机与嵌入式】公众号,回复关键字获取更多免费资料。回复【加群】,限时免费进入知识共享群;回复【3D封装库】,常用元器件的3D封装库;回复【电容】,获取电容、元器件选型相关的内容...

关键字: C51 MDK RealView

在Keil C51软件中51单片机的中断服务和外设驱动程序的开发

关键字: keil5 编译 C51

Intel公司1980年推出了MCS-51系列单片机:集成 8位CPU、4K字节ROM、128字节RAM、4个8位并口、1个全双工串行口、2个16位定时/计数器。寻址范围64K,并有控制功能较强的布尔处理器。 80C5...

关键字: C51 KEIL 编程

c上标3下标5怎么算用计算机,c上标3下标5怎么算

关键字: C51 KEIL

DSP28335与AD7606通过SPI的串行数据交互

关键字: keil C
关闭
关闭