搜索
bottom↓
回复: 25

STM8S003F3有没有办法使用printf,8kb的Flash

[复制链接]

出0入53汤圆

发表于 2017-10-31 12:04:31 | 显示全部楼层 |阅读模式
本帖最后由 zhcj66 于 2017-10-31 12:12 编辑

如题"STM8S003有没有办法使用printf,8kb的Flash"

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入4汤圆

发表于 2017-10-31 12:08:08 | 显示全部楼层
都可以的吧,加个标准库

出0入53汤圆

 楼主| 发表于 2017-10-31 12:11:46 | 显示全部楼层
huarana 发表于 2017-10-31 12:08
都可以的吧,加个标准库

我倒是想想库 一共8kb的空间,加入标准库放不下别东西了

出0入0汤圆

发表于 2017-10-31 12:15:54 | 显示全部楼层
zhcj66 发表于 2017-10-31 12:11
我倒是想想库 一共8kb的空间,加入标准库放不下别东西了

可以的呀,重定向一下,再选上microlib  full.

出100入101汤圆

发表于 2017-10-31 14:14:50 | 显示全部楼层
加入库,占不了多少空间

出0入0汤圆

发表于 2017-10-31 14:16:22 | 显示全部楼层
mini库是可以的,keil的话按照4楼所说就行

出0入362汤圆

发表于 2017-10-31 14:41:04 | 显示全部楼层
不需要浮点的话,用elm-chan的xprintf就好了
http://elm-chan.org/fsw/strf/xprintf.html


需要浮点的话。。。还是换rom大一点的mcu吧~

出0入0汤圆

发表于 2017-10-31 15:12:34 | 显示全部楼层
不用库,自己用串口封装出printf函数,足足够用

出0入53汤圆

 楼主| 发表于 2017-10-31 15:36:06 | 显示全部楼层
沉默胜过白金 发表于 2017-10-31 12:15
可以的呀,重定向一下,再选上microlib  full.

有工程?

出0入53汤圆

 楼主| 发表于 2017-10-31 15:36:30 | 显示全部楼层
hpu07 发表于 2017-10-31 15:12
不用库,自己用串口封装出printf函数,足足够用

是否有现成的?

出0入53汤圆

 楼主| 发表于 2017-10-31 15:37:20 | 显示全部楼层
tomzbj 发表于 2017-10-31 14:41
不需要浮点的话,用elm-chan的xprintf就好了
http://elm-chan.org/fsw/strf/xprintf.html

调试用,8k够用了,

出0入0汤圆

发表于 2017-10-31 19:02:00 | 显示全部楼层
本帖最后由 hpu07 于 2017-10-31 19:30 编辑


网上找到一个类似的:

#define   _INTSIZEOF(n)   ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1))
#define   va_start(ap,v)   (ap = (va_list)&v + _INTSIZEOF(v))
#define   va_arg(ap,t)     (*(t*)((ap += _INTSIZEOF(t)) -  _INTSIZEOF(t)))
#define   va_end(ap)      (ap = (va_list)0)
#define console_print(ch)    putchar(ch)

void print(char* fmt, ...)
{
    double vargflt = 0;
    int  vargint = 0;
    char* vargpch = NULL;
    char vargch = 0;
    char* pfmt = NULL;
    va_list vp;

    va_start(vp, fmt);
    pfmt = fmt;

    while(*pfmt)
    {
        if(*pfmt == '%')
        {
            switch(*(++pfmt))
            {
               
                case 'c':
                    vargch = va_arg(vp, int);
                    /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
                        mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
                    printch(vargch);
                    break;
                case 'd':
                case 'i':
                    vargint = va_arg(vp, int);
                    printdec(vargint);
                    break;
                case 'f':
                    vargflt = va_arg(vp, double);
                    /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
                        mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
                    printflt(vargflt);
                    break;
                case 's':
                    vargpch = va_arg(vp, char*);
                    printstr(vargpch);
                    break;
                case 'b':
                case 'B':
                    vargint = va_arg(vp, int);
                    printbin(vargint);
                    break;
                case 'x':
                case 'X':
                    vargint = va_arg(vp, int);
                    printhex(vargint);
                    break;
                case '%':
                    printch('%');
                    break;
                default:
                    break;
            }
            pfmt++;
        }
        else
        {
            printch(*pfmt++);
        }
    }
    va_end(vp);
}

void  printch(char ch)
{
    console_print(ch);
}

void printdec(int dec)
{
    if(dec==0)
    {
        return;
    }
    printdec(dec/10);
    printch( (char)(dec%10 + '0'));
}

void printflt(double flt)
{
    int icnt = 0;
    int tmpint = 0;
   
    tmpint = (int)flt;
    printdec(tmpint);
    printch('.');
    flt = flt - tmpint;
    tmpint = (int)(flt * 1000000);
    printdec(tmpint);
}

void printstr(char* str)
{
    while(*str)
    {
        printch(*str++);
    }
}

void printbin(int bin)
{
    if(bin == 0)
    {
        printstr("0b");
        return;
    }
    printbin(bin/2);
    printch( (char)(bin%2 + '0'));
}

void printhex(int hex)
{
    if(hex==0)
    {
        printstr("0x");
        return;
    }
    printhex(hex/16);
    if(hex < 10)
    {
        printch((char)(hex%16 + '0'));
    }
    else
    {
        printch((char)(hex%16 - 10 + 'a' ));
    }
}

网址:http://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html

出0入0汤圆

发表于 2017-10-31 21:32:51 来自手机 | 显示全部楼层
tomzbj 发表于 2017-10-31 14:41
不需要浮点的话,用elm-chan的xprintf就好了
http://elm-chan.org/fsw/strf/xprintf.html


这个不错呀

出0入53汤圆

 楼主| 发表于 2017-11-1 08:25:25 | 显示全部楼层
hpu07 发表于 2017-10-31 19:02
网上找到一个类似的:

#define   _INTSIZEOF(n)   ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ...

下面函数缺点当打印变量0时无法打印
void printdec(int dec)
{
    if(dec==0)
    {
        return;
    }
    printdec(dec/10);
    printch( (char)(dec%10 + '0'));
}

出0入53汤圆

 楼主| 发表于 2017-11-1 08:26:51 | 显示全部楼层
hpu07 发表于 2017-10-31 19:02
网上找到一个类似的:

#define   _INTSIZEOF(n)   ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ...

下面函数缺点,当是负数无法打印, 还有当小数位0.01 只能打印出来0.1这样打印是错误的
void printflt(double flt)
{
    int icnt = 0;
    int tmpint = 0;
   
    tmpint = (int)flt;
    printdec(tmpint);
    printch('.');
    flt = flt - tmpint;
    tmpint = (int)(flt * 1000000);
    printdec(tmpint);
}

出0入53汤圆

 楼主| 发表于 2017-11-1 08:27:54 | 显示全部楼层

浮点用什么函数打印比较好?

我用的下面函数

//打印float小数
void print_flt(double flt)
{
        //int icnt = 0;
        long tmpint = 0;
    unsigned char i = 0;
    if(flt<0)
    {
        my_Putc('-');
        flt = 0-flt;
    }
    else if(flt==0)
    {
        my_Putc('0');
    }
        tmpint = (int)flt;
    if(tmpint>1)        print_dec(tmpint);
    else if(flt>0)my_Putc('0');
        flt = flt - tmpint;
    if((flt!=0)&&(flt<1))
    {
        my_Putc('.');
        tmpint = (long)(flt * 1000000);
        
        for(i=6;i>0;i--)
        {
            flt *= 10;
            if((char)(flt)%10 == 0) my_Putc('0');
            else break ;
        }
        
        for(i=6;i>0;i--)
        {
            if(tmpint%10 == 0) tmpint /= 10;
            else break ;
        }
        print_dec(tmpint);
    }
}

出0入0汤圆

发表于 2017-11-1 08:43:29 | 显示全部楼层
你看看这个怎么样。这个是LPC1700库里面重构的printf函数,缓冲区不用那么多可以定义小点
void  _printf (const  char *format, ...)
{
    static  char  buffer[512 + 1];
            va_list     vArgs;
            char        *tmp;
    va_start(vArgs, format);
    vsprintf((char *)buffer, (char const *)format, vArgs);
    va_end(vArgs);

    _DBG(buffer);
}

出0入53汤圆

 楼主| 发表于 2017-11-1 09:54:10 | 显示全部楼层
weichao4808335 发表于 2017-11-1 08:43
你看看这个怎么样。这个是LPC1700库里面重构的printf函数,缓冲区不用那么多可以定义小点
void  _printf (c ...


还是不行的 section placement failed 部分放置失败,如果想用flash不够用

出0入0汤圆

发表于 2017-11-1 11:11:49 | 显示全部楼层
zhcj66 发表于 2017-11-1 09:54
还是不行的 section placement failed 部分放置失败,如果想用flash不够用

8K的flash就别用stdio库了,还是自己搞一个比较方便,就跟以前用89c52似的

出0入0汤圆

发表于 2017-11-1 11:21:21 | 显示全部楼层
用库函数的话,占用还是比较大的,8k有点偏小了,如果你的程序复杂的话,最好还是自己简单实现一个就可以调试用了

出0入53汤圆

 楼主| 发表于 2017-11-1 11:43:28 | 显示全部楼层
fsmcu 发表于 2017-11-1 11:21
用库函数的话,占用还是比较大的,8k有点偏小了,如果你的程序复杂的话,最好还是自己简单实现一个就可以调 ...

自己搞了一个

  1. void print_ch(char s)
  2. {
  3.     UART_Putc(s);
  4. //        buf[pp] = s;
  5. //        pp++;
  6. }

  7. void print_chs(char*s)
  8. {
  9.         while(*s)
  10.         {
  11.                 print_ch(*s++);
  12.         }
  13. }

  14. //字符串
  15. void print_str(char* str)
  16. {
  17.     print_chs(str);
  18. }

  19. //打印2进制数,打印2进制数,除0和负数
  20. void print_bin_0(int bin)
  21. {       
  22.         if(bin == 0)
  23.         {
  24.                 print_chs("0b");
  25.                 return;
  26.         }
  27.         print_bin_0(bin/2);
  28.         print_ch( (char)(bin%2 + '0'));
  29. }

  30. //打印2进制数
  31. void print_bin(int bin)
  32. {       
  33.         if(bin == 0)
  34.         {
  35.                 print_chs("0b0");
  36.                 return;
  37.         }
  38.     if(bin<0)
  39.     {
  40.         print_ch('-');
  41.         bin = 0-bin;
  42.     }
  43.         print_bin_0(bin);
  44. }

  45. //打印10进制数,除0和负数
  46. void print_dec_0(long dec)
  47. {
  48.         if(dec==0)
  49.         {
  50.         return;
  51.         }
  52.         print_dec_0(dec/10);
  53.         print_ch( (char)(dec%10 + '0'));
  54. }

  55. //打印10进制数
  56. void print_dec(long dec)
  57. {
  58.    
  59.         if(dec==0)
  60.         {
  61.         print_ch('0');
  62.         return;
  63.         }
  64.     if(dec<0)
  65.     {
  66.         print_ch('-');
  67.         dec = 0-dec;
  68.     }
  69.         print_dec_0(dec);
  70. }

  71. //打印float小数
  72. void print_flt(double flt)
  73. {
  74.         //int icnt = 0;
  75.         long tmpint = 0;
  76.     unsigned char i = 0;
  77.     if(flt<0)
  78.     {
  79.         print_ch('-');
  80.         flt = 0-flt;
  81.     }
  82.     else if(flt==0)
  83.     {
  84.         print_ch('0');
  85.         return;
  86.     }
  87.         tmpint = (int)flt;
  88.     if(tmpint>=1)    print_dec(tmpint);
  89.     else if(flt>0)   print_ch('0');
  90.         flt = flt - tmpint;
  91.     if((flt!=0)&&(flt<1))
  92.     {
  93.         print_ch('.');
  94.         tmpint = (long)(flt * 1000000);
  95.         
  96.         for(i=6;i>0;i--)
  97.         {
  98.             flt *= 10;
  99.             if((char)(flt)%10 == 0) print_ch('0');
  100.             else break ;
  101.         }
  102.         
  103.         for(i=6;i>0;i--)
  104.         {
  105.             if(tmpint%10 == 0) tmpint /= 10;
  106.             else break ;
  107.         }
  108.         print_dec(tmpint);
  109.     }
  110. }

  111. //以16进制打印,除0和负数
  112. void print_hex_0(long hex)
  113. {
  114.         if(hex==0)
  115.         {
  116.                 print_chs("0x");
  117.                 return;
  118.         }
  119.         print_hex_0(hex/16);
  120.     hex %= 16;
  121.         if(hex < 10)
  122.         {
  123.                 print_ch((char)((hex%16) + '0'));
  124.         }
  125.         else
  126.         {
  127.                 print_ch((char)((hex%16) - 10 + 'A' ));
  128.         }
  129. }

  130. //以16进制打印
  131. void print_hex(long hex)
  132. {
  133.         if(hex==0)
  134.         {
  135.                 print_chs("0x0");
  136.                 return;
  137.         }
  138.     if(hex<0)
  139.     {
  140.         print_ch('-');
  141.         hex = 0-hex;
  142.     }
  143.         print_hex_0(hex);
  144. }

  145. void print(char* fmt, ...)
  146. {
  147.     double vargflt = 0;
  148.     int  vargint = 0;
  149.     char* vargpch = NULL;
  150.     char vargch = 0;
  151.     char* pfmt = NULL;
  152.     va_list vp;

  153.     va_start(vp, fmt);
  154.     pfmt = fmt;

  155.     while(*pfmt)
  156.     {
  157.         if(*pfmt == '%')
  158.         {
  159.             switch(*(++pfmt))
  160.             {
  161.                
  162.                 case 'c':
  163.                     vargch = va_arg(vp, int);
  164.                     /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
  165.                         mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
  166.                     print_ch(vargch);
  167.                     break;
  168.                 case 'd':
  169.                 case 'i':
  170.                     vargint = va_arg(vp, int);
  171.                     print_dec(vargint);
  172.                     break;
  173.                 case 'f':
  174.                     vargflt = va_arg(vp, double);
  175.                     /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
  176.                         mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
  177.                     print_flt(vargflt);
  178.                     break;
  179.                 case 's':
  180.                     vargpch = va_arg(vp, char*);
  181.                     print_str(vargpch);
  182.                     break;
  183.                 case 'b':
  184.                 case 'B':
  185.                     vargint = va_arg(vp, int);
  186.                     print_bin(vargint);
  187.                     break;
  188.                 case 'x':
  189.                 case 'X':
  190.                     vargint = va_arg(vp, int);
  191.                     print_hex(vargint);
  192.                     break;
  193.                 case '%':
  194.                     print_ch('%');
  195.                     break;
  196.                 default:
  197.                     break;
  198.             }
  199.             pfmt++;
  200.         }
  201.         else
  202.         {
  203.             print_ch(*pfmt++);
  204.         }
  205.     }
  206.     va_end(vp);
  207. }
复制代码

出0入53汤圆

 楼主| 发表于 2017-11-1 13:15:30 | 显示全部楼层
本帖最后由 zhcj66 于 2017-11-2 09:12 编辑
weichao4808335 发表于 2017-11-1 11:11
8K的flash就别用stdio库了,还是自己搞一个比较方便,就跟以前用89c52似的


IAR stm8的编译器怎么
工程调试的时候
MY_Fuzzy.T_P_U_V_3 = 50; f_error =18;
if(f_error<(-MY_Fuzzy.T_P_U_V_3))
{
        d_temp = 0;
}
怎么判断是真,在mdk是正常的
感觉这个IAR编译器 不按常理 判断 怎么回事?
MY_Fuzzy.T_P_U_V_3   是unsigned short

f_error是float

难道这个IAR对数据类型还要要求?

改为下面就可以了
if(f_error<(-(short)MY_Fuzzy.T_P_U_V_3))
{
        d_temp = 0;
}

出10入46汤圆

发表于 2017-11-2 11:34:42 | 显示全部楼层
hpu07 发表于 2017-10-31 19:02
网上找到一个类似的:

#define   _INTSIZEOF(n)   ((sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) ...


tmpint = (long)(flt * 1000000);
如上浮点数中的代码,实际上已经丧失了,打印的数值的精度。一种粗略实现而已。

出0入53汤圆

 楼主| 发表于 2017-11-2 16:20:44 | 显示全部楼层
gonboy 发表于 2017-11-2 11:34
tmpint = (long)(flt * 1000000);
如上浮点数中的代码,实际上已经丧失了,打印的数值的精度。一种粗略 ...

是啊精度不行,调试用可以接受,你有好的办法?

出0入17汤圆

发表于 2017-11-2 17:06:04 | 显示全部楼层
可以,但是没必要啊,内存这么小

出0入0汤圆

发表于 2017-11-2 18:03:21 | 显示全部楼层
gonboy 发表于 2017-11-2 11:34
tmpint = (long)(flt * 1000000);
如上浮点数中的代码,实际上已经丧失了,打印的数值的精度。一种粗略 ...

要搞功能完整版的,不知道要补多少库函数了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-6-3 20:50

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表