在嵌入式中,经常需要用到printf来调试程序

标准库函数的默认输出设备是显示器,要实现在串口或LCD输出,必须重定义标准库函数里调用的与输出设备相关的函数.

printf输出到串口,需要将fputc里面的输出指向串口(重定向)

因此,实现printf就需要重定向相关的函数。有的时候,我们想自己实现printf的功能,不妨自己写一个

#include <stdarg.h>
#include <stdio.h>
#include <unistd.h> #define BUFSIZE 1024
char myprintf_buf[BUFSIZE]; void Debug_printf(const char* fmt, ...)
{
va_list args;
int n; va_start(args, fmt);
n = vsnprintf(myprintf_buf, BUFSIZE, fmt, args);
va_end(args);
int i = ;
for(i = ; i < n; i++)
{
HAL_UART_Transmit(&huart2, (uint8_t *)&myprintf_buf[i], , 0xFFFF); //根据不同的平台,修改串口输出的函数
}
} int main(void)
{
Debug_printf("test %d\r\n", );
return ;
}

方法二:

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
static unsigned long m_pow(int x,int y)
{
  unsigned long sum = ;
  while(y--)
  {
    sum *= x;
  }
  return sum;
}
//打印字符​
void m_putchar(const char ch)
{
  HAL_UART_Transmit(&huart2, (uint8_t *)ch, , 0xFFFF);
}
//打印字符串
void m_putstr(const char *str)
{
  while(*str)
  {
    m_putchar(*str++);
  }
}
int printint(const int dec)
{
  int count = , ret = ;
  int r_val = dec;
  char ch;
  while(r_val)
  {
    count++;
    r_val /= ;
  }
  ret = count;
  r_val = dec;
  while(count)
  {
    ch = r_val / m_pow(, count - );
    r_val %= m_pow(, count - );
    m_putchar(ch + '');
    count--;
  }
  return ret;
}
int printfloat(const float flt)
{
  int countint = ,countflt = ;
  int count = , r_val = ;
  int tmpint = (int)flt;
  int tmpflt = (long int)( * (flt - tmpint));
  if(tmpflt % >= )
  {
    tmpflt = tmpflt / + ;
  }
  else
  {
    tmpflt = tmpflt / ;
  }
  r_val = tmpflt;
  count = ;
  while(r_val)
  {
    count++;
    r_val /= ;
  }   countint = printint(tmpint);
  m_putchar('.');   int i = ;
  for(i = ; i < - count; i++)
  {
    m_putchar('');
  }
  countflt = printint(tmpflt);
  return countint + + count + countflt;
}
int m_printf(const char *str,...)
{
va_list ap;
int val,r_val;
float val_float;
char count,ch;
char *s = NULL;
int res = ;
va_start(ap, str);
while('\0' != *str)
{
 switch(*str)
{
case '%':
 str++;
 switch(*str)
 {
 case 'd':
   val = va_arg(ap, int);
count = printint(val);
res += count;
break;
case 'x':
val = va_arg(ap, int);
r_val = val;
count = ;
while(r_val)
{
  count++;
  r_val /= ;
}
res += count;
r_val = val;
while(count)
{
  ch = r_val / m_pow(, count - );
  r_val %= m_pow(, count - );
if(ch <= )
  m_putchar(ch + '');
else
  m_putchar(ch - + 'a');
count--;
}
break;
case 'f':
val_float = va_arg(ap, double);
count = printfloat(val_float);
res += count;
break;
case 's':
s = va_arg(ap, char*);
m_putstr(s);
res += strlen(s);
break;
case 'c':
m_putchar((char)va_arg(ap, int));
res += ;
break;
default:
 ;
}
case '\n':
 m_putchar('\n');
 res += ;
 break;
case '\r':
 m_putchar(*str);
 res += ;
 break;
default:
 m_putchar(*str);
 res += ;
 }
 str++;
 }
 va_end(ap);
 return res;
} int main(void)
{
char buf[];
m_printf("======== begin test =========\n");
snprintf(buf, sizeof(buf), "%s %f", "hello world", 20.043);
m_printf("%s\n", buf);
return ;
}

嵌入式 printf的实现的更多相关文章

  1. 嵌入式 printf函数

    来自:https://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html #include <stdio.h> #includ ...

  2. OpenCL Hello World

    ▶ OpenCL 的环境配置与第一个程序 ● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行:AMD 需要下载 AMD APP SDK(https://community.a ...

  3. 嵌入式操作系统---打印函数(printf/sprintf)的实现

    一.打印函数简介 作用:将“给定的内容”按照“指定的格式”输出到“指定目标内”. 打印函数的基本格式: char print_buf[BUF_SIZE]; void printf(const char ...

  4. [misc]如何在嵌入式平台使用printf功能

    转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html 摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上, ...

  5. 嵌入式处理器通过UART实现scanf和printf

    #include <stdint.h> #include <stdarg.h> extern int vsscanf(const char *, const char *, v ...

  6. 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...

  7. 可变参数列表与printf()函数的实现

    问题 当我们刚开始学习C语言的时候,就接触到printf()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...

  8. 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验

    20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...

  9. sqlite嵌入式数据库C语言基本操作(2)

    :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...

随机推荐

  1. Java入门笔记 03-面向对象(下)

    介绍:除了前面介绍的关于类.对象的基本语法之外,下面继续介绍Java面向对象的特性. 一. 包装类: 从JDK 1.5以后,Java就提供了自动装箱和自动拆箱操作,即: 自动装箱:将一个基本类型的变量 ...

  2. socket实现简单的FTP

    一.开发环境 server端:centos 7  python-3.6.2 客户端:Windows 7 python-3.6.2 pycharm-2018 程序目的:1.学习使用socketserve ...

  3. DL4J之CNN对今日头条文本分类

    一.数据集介绍 数据来源:今日头条客户端 数据格式如下: 6551700932705387022_!_101_!_news_culture_!_京城最值得你来场文化之旅的博物馆_!_保利集团,马未都, ...

  4. PHP中数字转为百分位,千分位,万分位。。。

    今天做项目中,需要将文章点击量显示在页面中,需求中给的是多少多少万,虽然不是什么难事,但做程序员这么久了,需要考虑的不再是简单的实现,而且有效率和快捷, 虽然PHP自带的函数有number_forma ...

  5. openjudge(POJ)-1664 放苹果

    对于n个盘子,m个苹果,我们要么在每个盘子上都放苹果,要么至少有一个盘子不放. 一个盘子不放就是f(m,n-1),全部都放的时候苹果就变成了n-m个,但是盘子的数目是不变的,因为此时还没有产生方案数, ...

  6. Nexus-vPC理论

    vPC:virtual Port-channel 1.vPC的作用: • 允许一个设备使用2个上游的设备的端口来实现Port Channel    • 消除STP阻止端口的情况    • 提供一个无环 ...

  7. 【转】使用shell登录远程服务器执行多条命令,ssh登录之后执行脚本文件

    原文:https://blog.csdn.net/qq_36622490/article/details/100773589 这个需求主要是我在jenkins中pipeline的代码里,需要使用she ...

  8. iOS一个简单的设置圆角不引起性能问题的分类

    http://www.cocoachina.com/articles/18756 iOS设置圆角矩形和阴影效果 https://www.cnblogs.com/rayshen/p/4900336.ht ...

  9. 都客仿站高手已注册旗舰版V3.1

    链接:https://pan.baidu.com/s/1R5ldFDjekuXmEp42-8SQSQ 提取码:gkm9

  10. Java记录2---包的使用

    javac -d . A.java -d 表示自动生成包层 . 表示这个包层在当前目录下建立 package link.roland;//package 语句必须是第一条语句 //该语句表示把该文件中 ...