[技术问答] 51单片机的Printf使用方法

[复制链接]
 楼主| geraldbetty 发表于 2025-1-12 21:13 | 显示全部楼层 |阅读模式



  1. /*
  2. 附:利用单片机和PC机的串口通信的工作方式1(波特率可变),(已知晶振为11.0592MHZ)设置通信波特率为9600b/s,结合定时器1的工作方式2(TH1=0xfd,TL1=0xfd),输入上底、下底、高,输出梯形面积
  3.        
  4.         提示:下载程序后,打开串口工具,需要复位一下。
  5. */

  6. #include<reg52.h>
  7. #include<stdio.h>

  8. #define uint unsigned int
  9. #define uchar unsigned char

  10. void uart(void)                                        //串口通信初始化
  11. {
  12.         SCON=0x40;                                        //串口通信工作方式1
  13.         REN=1;                                                //允许接收
  14.         TMOD=0x20;                                        //定时器1的工作方式2
  15.         TH1=0xfd,TL1=0xfd;               
  16.         TI=1;                       //这里一定要注意
  17.         TR1=1;                                                //打开定时器
  18. }                                                               
  19. void main(void)
  20. {
  21.         uint up=0,down=0,high=0;
  22.         uint result ;
  23.         uart();
  24.         while(1)
  25.                 {        
  26.                         printf("********************************\r\n");
  27.                         printf("Please input up,dowm,high:\r\n");
  28.                         scanf("%d%d%d",&up,&down,&high)        ;
  29.                         printf("\r\nResult=%d\r\n",result=(up+down)*high/2);
  30.                 }
  31. }


小灵通2018 发表于 2025-1-13 18:53 | 显示全部楼层
函数如何实现的?
jasontu 发表于 2025-1-14 13:34 | 显示全部楼层

keil c51 以输出int型变量为例:

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

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.

%.1f(输出保留1位小数)

e        float        Floating-point number formatted as<[>-<]>d.dddde<[>-<]>dd.
E        float        Floating-point number formatted as<[>-<]>d.ddddE<[>-<]>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.
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.
Stahan 发表于 2025-1-19 23:37 来自手机 | 显示全部楼层
这个会不会占太多空间啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

1611

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

36

主题

1611

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部