打印
[STM32F1]

C中的sprintf函数

[复制链接]
442|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ulystronglll|  楼主 | 2024-6-29 07:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
常用sprint函数来将数值转换为字符串。可在Keil C中使用这个函数时就要注意了。请看下面例程:

/*main.c
  Designed by Bill Liu
  Version 0.0
  Modified last by Bill Liu on 12/19/2021
*/


#include "main.h"
ui8 tem;
ui16 tem1;
float tem2;
void main()
{
       
  STCIO_InitPortsBits(P0|P1|P2|P3|P4, 0xFF, BI_IO);
        P3 = 0xFF;
        P0 = 0xFF;
  UartS1_Init(VBAUD_8BITS,G1,T2,9600);
        STCIO_InitP3Bit(SCT_BIT1, PP_OUT);
       
        SundBuzzerx10ms(50);
        Delay10xms(50, FSCLK);
        SundBuzzerx10ms(50);
        tem = 123;
  tem1 = 123;
  tem2 = 123;
  while(1)
  {
    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem);
    UartS1_SendString(mstr);

    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem1);
    UartS1_SendString(mstr);

    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem2);
    UartS1_SendString(mstr);
    Delay10xms(200, FSCLK);

  }
}
//End of main()

//*********************************************
void SundBuzzerx10ms(ui8 x)
{
        BUZZER = 0;
        Delay10xms(x, FSCLK);
        BUZZER = 1;
}
//End of SundBuzzerx10ms(ui8 x)
看下在串口助手上的输出结果:



显然只有在变量类型为ui16时,结果正确。也就是说只能正确解析16位数。再看下面例程:

/*main.c
  Designed by Bill Liu
  Version 0.0
  Modified last by Bill Liu on 12/19/2021
*/


#include "main.h"
ui8 tem;
ui16 tem1;
float tem2;
void main()
{
       
  STCIO_InitPortsBits(P0|P1|P2|P3|P4, 0xFF, BI_IO);
        P3 = 0xFF;
        P0 = 0xFF;
  UartS1_Init(VBAUD_8BITS,G1,T2,9600);
        STCIO_InitP3Bit(SCT_BIT1, PP_OUT);
       
        SundBuzzerx10ms(50);
        Delay10xms(50, FSCLK);
        SundBuzzerx10ms(50);
        tem = 123;
  tem1 = 123;
  tem2 = 123;
  while(1)
  {
    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", 123);
    UartS1_SendString(mstr);

    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem1);
    UartS1_SendString(mstr);
    /*
    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem2);
    UartS1_SendString(mstr);
    */
    Delay10xms(200, FSCLK);

  }
}
//End of main()

//*********************************************
void SundBuzzerx10ms(ui8 x)
{
        BUZZER = 0;
        Delay10xms(x, FSCLK);
        BUZZER = 1;
}
//End of SundBuzzerx10ms(ui8 x)
再看下在串口助手上的输出结果:



显然直接输入数字也不能被正确解析。再看一个例程:

/*main.c
  Designed by Bill Liu
  Version 0.0
  Modified last by Bill Liu on 12/19/2021
*/


#include "main.h"
ui8 tem;
ui16 tem1;
float tem2;
void main()
{
       
  STCIO_InitPortsBits(P0|P1|P2|P3|P4, 0xFF, BI_IO);
        P3 = 0xFF;
        P0 = 0xFF;
  UartS1_Init(VBAUD_8BITS,G1,T2,9600);
        STCIO_InitP3Bit(SCT_BIT1, PP_OUT);
       
        SundBuzzerx10ms(50);
        Delay10xms(50, FSCLK);
        SundBuzzerx10ms(50);
        tem = 123;
  tem1 = 123;
  tem2 = 123;
  while(1)
  {
    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", 305);
    UartS1_SendString(mstr);

    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem1);
    UartS1_SendString(mstr);
    /*
    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%d\r\n", tem2);
    UartS1_SendString(mstr);
    */
    Delay10xms(200, FSCLK);

  }
}
//End of main()

//*********************************************
void SundBuzzerx10ms(ui8 x)
{
        BUZZER = 0;
        Delay10xms(x, FSCLK);
        BUZZER = 1;
}
//End of SundBuzzerx10ms(ui8 x)
串口助手上的输出结果:



可以看出输入的305可以被正确解析。也就是说如果输入的是一个为声明类型的数值必须是个16位数据才能被解析。 再看下面例程:

/*main.c
  Designed by Bill Liu
  Version 0.0
  Modified last by Bill Liu on 12/19/2021
*/


#include "main.h"
ui8 tem;
ui16 tem1;
float tem2;
void main()
{
       
  STCIO_InitPortsBits(P0|P1|P2|P3|P4, 0xFF, BI_IO);
        P3 = 0xFF;
        P0 = 0xFF;
  UartS1_Init(VBAUD_8BITS,G1,T2,9600);
        STCIO_InitP3Bit(SCT_BIT1, PP_OUT);
       
        SundBuzzerx10ms(50);
        Delay10xms(50, FSCLK);
        SundBuzzerx10ms(50);
        tem = 123;
  tem1 = 123;
  tem2 = 123;
  while(1)
  {
    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%.2f\r\n", 305);
    UartS1_SendString(mstr);

    memset(mstr,0,strlen(mstr));
    sprintf(mstr,"%.2f\r\n", tem2);
    UartS1_SendString(mstr);
    Delay10xms(200, FSCLK);

  }
}
//End of main()

//*********************************************
void SundBuzzerx10ms(ui8 x)
{
        BUZZER = 0;
        Delay10xms(x, FSCLK);
        BUZZER = 1;
}
//End of SundBuzzerx10ms(ui8 x)
串口助手上的输出结果:



直接输入数305,不能被正确解析。 由此可见Keil C中的sprintf用作将数值装换位字符串并不是很好用,有必要自己写一个库函数程序来完成这一工作。函数声明如下:

/***********************************************************************
Function: FloatString(f64 src, i8* desString, ui8 mplace);
Return value: void
Discription: covert decimal digit & decimal places of an  float data  to string
Example:
  ui8 tem = 12345;
  ui8 mstr[12] = {0};
  FloatString(tem,mstr,2); //"12345.00"
***********************************************************************/
void FloatString(f64 src, i8* desString, ui8 mplace);
实现代码:

void FloatString(f64 src, i8* desString, ui8 mplace)
{
f64 ftem = fabs( src);
ui8 iplaces = 1;
ui8 i = 0;
if(ftem>= 10.0)
{
   while(ftem>= 10.0)
   {
      iplaces++;
     ftem /= 10;
   }
}
if(src < 0)
{
   desString[0] = '-';
   i = 1;
   iplaces++;
}
for(; i < iplaces + mplace + 1; i++)
{
   if(i == iplaces)
   {
     if(mplace)
     {
       desString[i] = '.';
       continue;
     }
     else
       break;
   }
   else
   {
     desString[i] = (ui8)ftem;
     ftem -= desString[i];
     desString[i] += '0';
     ftem *= 10;
   }
}
}
//End of FloatString(f64 src, i8* desString, ui8 mplace)
使用示例:

头文件

/*main.h
  Designed by Bill Liu
  Version 0.0
  Modified last by Bill Liu on 12/19/2021
*/

#ifndef __MAIN_H_
#define __MAIN_H_

#include "mtype.h"
#include "delay.h"
#include "stcuart.h"
#include "stcio.h"
#include "myport.h"


const FSYSCLOCK FSCLK = F30MHz; //define system clock frequency 30MHz, FSYSCLOCK was defined in mtype.h
const ui32  FSCLK1 = 30000000L; //define system clock frequency 30MHz
ui16 code Vbg_ROM _at_ 0xf3f7;  //band gap voltage stord at memory end address -8

i8 mstr[25] ="";


void SundBuzzerx10ms(ui8 x);



#endif
源文件:

/*main.c
  Designed by Bill Liu
  Version 0.0
  Modified last by Bill Liu on 12/19/2021
*/


#include "main.h"
ui8 tem;
ui16 tem1;
float tem2;
void main()
{
       
  STCIO_InitPortsBits(P0|P1|P2|P3|P4, 0xFF, BI_IO);
        P3 = 0xFF;
        P0 = 0xFF;
  UartS1_Init(VBAUD_8BITS,G1,T2,9600);
        STCIO_InitP3Bit(SCT_BIT1, PP_OUT);
       
        SundBuzzerx10ms(50);
        Delay10xms(50, FSCLK);
        SundBuzzerx10ms(50);
        tem = 123;
  tem1 = 123;
  tem2 = -123.123;
  while(1)
  {
    memset(mstr,0,strlen(mstr));
    FloatString(tem, mstr, 0);
    UartS1_SendString(mstr);
    UartS1_SendString("\r\n");

    memset(mstr,0,strlen(mstr));
    FloatString(tem1, mstr, 1);
    UartS1_SendString(mstr);
    UartS1_SendString("\r\n");

    memset(mstr,0,strlen(mstr));
    FloatString(tem2, mstr, 3);
    UartS1_SendString(mstr);
    UartS1_SendString("\r\n");
    Delay10xms(200, FSCLK);
  }
}
//End of main()

//*********************************************
void SundBuzzerx10ms(ui8 x)
{
        BUZZER = 0;
        Delay10xms(x, FSCLK);
        BUZZER = 1;
}
//End of SundBuzzerx10ms(ui8 x)
串口助手上的输出结果:



有了这个函数,要将数值输出为字符串显示就很方便了。


使用特权

评论回复
沙发
huahuagg| | 2024-6-29 15:44 | 只看该作者
格式化打印到字符串。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

1233

帖子

1

粉丝