简易printf打印实现,占用内存非常小------<嵌入式开发自学笔记>

//打印单个字符
void print_ch(const char ch)
{
//这里实现你的串口发送单个字符的函数
  // LPUART_WriteBlocking(LPUART0, (uint8_t *)&ch, 1);
}

//打印整数,不明白的可以网上查查,怎么回事,print_int()又调用了print_int()
void print_int(int dec)
{
    if(dec < 0)
    {
        print_ch('-');
        dec = -dec;
    }
    if(dec / 10)
        print_int(dec / 10);
    print_ch(dec%10 + '0');
}
//转换成十六进制

static void get_hex(uint8_t hex)
{
    const uint8_t ascii_zero = 48;
    const uint8_t ascii_a = 65;

    if ((hex >= 0) && (hex <= 9))
    {
        print_ch(hex + ascii_zero);
    }
    if ((hex >= 10) && (hex <= 15))
    {
        print_ch(hex - 10 + ascii_a);
    }
}
//以十六进制格式输出
void print_hex(uint32_t hex)
{
    if(hex / 16)
        print_hex(hex/16);
    get_hex(hex%16);
}

//打印字符串
void print_str(const char *ptr)
{
    while(*ptr)
    {
        print_ch(*ptr);
        ptr++;
    }
}

//打印浮点
void print_float(const float flt)
{
    int tmpint = (int)flt;
    int tmpflt = (int)(100000 * (flt - tmpint));
    if(tmpflt % 10 >= 5)
    {
        tmpflt = tmpflt / 10 + 1;
    }
    else
    {
        tmpflt = tmpflt / 10;
    }
    print_int(tmpint);
    print_ch('.');
    print_int(tmpflt);

}

//带格式打印,
void my_printf(const char *format,...)
{
    va_list ap;
    va_start(ap,format);
    while(*format)
    {
        if(*format != '%')
        {
            print_ch(*format);
            format++;
        }
        else
        {
            format++;
            switch(*format)
            {
            case 'c':
            {
                char valch = va_arg(ap,int);
                print_ch(valch);
                format++;
                break;
            }
            case 'd':
            {
                int valint = va_arg(ap,int);
                print_int(valint);
                format++;
                break;
            }
            case 's':
            {
                char *valstr = va_arg(ap,char *);
                print_str(valstr);
                format++;
                break;
            }
            case 'f':
            {
                float valflt = va_arg(ap,double);
                print_float(valflt);
                format++;
                break;
            }
            case 'x':
            case 'X':
            {
                int valhex = va_arg(ap,int);

                if(((uint32_t)valhex)<16)
                {
                    print_ch('0');
                }
                print_hex((uint32_t)valhex);
                format++;
                break;
            }
            default:
            {
                print_ch(*format);
                format++;
            }
            }
        }
    }
    va_end(ap);
}













  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值