嵌入式 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 ...
随机推荐
- 「NOI2016」区间
传送门 Luogu 解题思路 对于选出的区间,我们可以直接用线段树维护区间内单点被覆盖次数最大值. 那么解题重心便落在了选取方式上. 为了让最大值最小,考虑尺取,不能二分,降低效率而且不好写. 先将区 ...
- 如何使用canvas绘制椭圆,扩展非chrome浏览器中的ellipse方法
这篇博文主要针对浏览器中绘制椭圆的方法扩展.在网上搜索了很多,发现他们绘制椭圆的方式都有缺陷.其中有压缩法,计算法,贝塞尔曲线法等多种方式.但是都不能很好的绘制出椭圆.所有我就对这个绘制椭圆的方式进行 ...
- Plastic Bottle Manufacturer Tips - Attention To Plastic Bottle Processing Technology
In fact, the processing technology of plastic bottles is actually quite strict. In fact, regular man ...
- Python:re 模块
re模块是python内置的正则表达式模块
- java里自定义分页查询的尝试
public String list(){ try { LoginUser loginUser = getLoginUser();//获取当前登录用户 if(curpage<=0){ curpa ...
- leetcode 0207
目录 ✅ 561. 数组拆分 I ✅ 1025. 除数博弈 聪明的数学归纳法: 动态规划又来了(没理解,todo 0207): ✅ 557. 反转字符串中的单词 III py 中的 字符 split ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- selenium+chrome options
selenium+chrome options 环境:selenium chrome 1. selenium + chrome参数配置 1.1. 启动 from selenium im ...
- mysql 连接数据库时时区报错
1.url: jdbc:mysql://192.168.0.101:3306/testdb?serverTimezone=UTC,在连接字符后面添加时区设置 2.使用navicat或者直接使用mysq ...
- docker的私有化仓库harbor搭建
目前比较流行的docker私有化仓库是harbor,harbor是一个github开源的项目,直接在github上搜索即可,下载地址:https://github.com/goharbor/harbo ...