嵌入式 printf的实现
在嵌入式中,经常需要用到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的实现的更多相关文章
- 嵌入式 printf函数
来自:https://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html #include <stdio.h> #includ ...
- OpenCL Hello World
▶ OpenCL 的环境配置与第一个程序 ● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行:AMD 需要下载 AMD APP SDK(https://community.a ...
- 嵌入式操作系统---打印函数(printf/sprintf)的实现
一.打印函数简介 作用:将“给定的内容”按照“指定的格式”输出到“指定目标内”. 打印函数的基本格式: char print_buf[BUF_SIZE]; void printf(const char ...
- [misc]如何在嵌入式平台使用printf功能
转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html 摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上, ...
- 嵌入式处理器通过UART实现scanf和printf
#include <stdint.h> #include <stdarg.h> extern int vsscanf(const char *, const char *, v ...
- 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...
- 可变参数列表与printf()函数的实现
问题 当我们刚开始学习C语言的时候,就接触到printf()函数,可是当时"道行"不深或许不够细心留意,又或者我们理所当然地认为库函数规定这样就是这样,没有发现这个函数与普通的函数 ...
- 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验
20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...
- sqlite嵌入式数据库C语言基本操作(2)
:first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...
随机推荐
- 「CF241E」Flights
传送门 Luogu 解题思路 首先对于所有不属于任何一条路径上的边,它的权值是任意的. 对于所有在路径上的边 \((u,v)\) 满足 \(1\le dis_v-dis_u\le2\) 差分约束即可. ...
- FileUpload之FileItem类的常用方法
http://blog.csdn.net/chinaliuyan/article/details/7002014
- 最全BT磁力搜索引擎索引(整理分享,每日更新)
btaa.xyz:http://www.veee.xyz/(可以访问,知名的BT磁力搜索,资源多,建议手机访问) 以下无法访问 idope.se:https://idope.se/(无法访问,资源丰富 ...
- 端口打开和关闭do while
;Author : Bing Song ;// ;Usage: modify “logfile" according to actual drictory getdir logdir #获取 ...
- Mayor's posters-POJ2528 区间染色+离散化
题意: 在一面长度为10000000 的墙上贴广告,告诉你每张海报的l,r(1 <= li <= ri <= 10000000.),让你求最后有几张海报露出来 链接:http://p ...
- 使用tag标签是SEO优化的重要性是什么?
使用tag标签是SEO优化的重要性是什么? tag标签是一种SEO技术,在网站优化的过程中,更准确.更具体地用关键词对文章进行分类,对SEO优化具有重要的作用. 但是,很多新人站长在发表文章时不太注意 ...
- 【Python数据挖掘】第六篇--特征工程
一.Standardization 方法一:StandardScaler from sklearn.preprocessing import StandardScaler sds = Standard ...
- Codeforces Round #588 (Div. 2)D(思维,多重集)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[7007],b[700 ...
- java 获取web登录者的ip地址
/** * 获取访问用户的客户端IP(适用于公网与局域网). */ public static final String getIpAddr(final HttpServletRequest requ ...
- 移动端一像素边框解决方案[css scale]
新建一个border.css的文件,然后将代码复制粘贴,然后引用border.css样式文件,然后给需要添加边框的元素,加相应的类样式. tips: border-bottom[一像素下边框]:bor ...