当前位置:网站首页>单片机不支持printf 浮点类型的解决办法
单片机不支持printf 浮点类型的解决办法
2022-07-21 13:14:00 【ba_wang_mao】
在一些单片机中因为资源受限官方提供的SDK中通常不提供浮点类型数据的打印。如需浮点打印功能可以参考如下做法:
前提条件
1、串口通信正常
2、printf 函数已经移植完成
操作方法
1、添加以下函数到工程,以下函数的作用为将一个float类型函数转换为一个字符串。
2、将转换完成的字符串通过printf("%s");进行输出打印。
/**
* @brief 将float类型数据转换为字符串输出
*
* @param num [IN] 要转换的数据
* @param n [IN] 小数位的个数
* @return uint8_t* [OUT] 转换完成的字符串的
*/
uint8_t *float2str(double num, int n)
{
static uint8_t num_str[13] = {
'\0'};
uint8_t len = 0;
uint8_t n_max = 0; //小数点最大位数
uint32_t i = 0;
for (i = 0; i < 13; i++) //字符串清空
num_str[i] = '\0';
//确定符号
if (num > 0)
{
num_str[0] = '+';
}
else if (num < 0)
{
num_str[0] = '-';
num = -num;
}
if ((int)(num / 100000) != 0) //确定整数部分长度
{ // 6位数
len = 7;
n_max = 4;
}
else if ((int)(num / 10000) != 0)
{ // 5位数
len = 6;
n_max = 5;
}
else if ((int)(num / 1000) != 0)
{ // 4位数
len = 5;
n_max = 6;
}
else if ((int)(num / 100) != 0)
{ // 3位数
len = 4;
n_max = 7;
}
else if ((int)(num / 10) != 0)
{ // 2位数
len = 3;
n_max = 8;
}
else
{ // 1位数
len = 2;
n_max = 9;
}
if (n > n_max) //小数点限幅
n = n_max;
len += n; //确定长度
i = n; //变成整数
while (i--)
num = (num * 10);
while (n--) //转换小数
{
num_str[len--] = (int)num % 10 + 0x30;
num = num / 10;
}
num_str[len] = '.';
while (len--) //转换整数
{
num_str[len] = (int)num % 10 + 0x30;
num = num / 10;
if (len == 1)
break;
}
return num_str;
}
————————————————
版权声明:本文为CSDN博主「Argon_Ghost」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Argon_Ghost/article/details/123787706
边栏推荐
- 如何使用多类型数据预训练多模态模型?
- LeetCode-153-寻找旋转排序数组中的最小值
- [chestnut sugar GIS] how to batch delete empty files with the same name in multiple folders
- PL/SQL 记录
- PL/SQL 异常
- [chestnut sugar GIS] record two stupid records - desktop computer has no signal or does not light up
- [chestnut sugar GIS] bat - how to rename the data in sub files in batch
- Yousi College | learn six sigma management from Sun Tzu's art of war
- Operation of Py file
- Let Matplotlib and Seaborn data map move ~
猜你喜欢
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (6)
[wechat applet] Introduction to wxss and global and page configuration
How to pre train multimodal models using multi type data?
window 系统里 chrome 浏览器一些实用的调试技巧
Windows cannot start the mysql80 service (located on the local computer)
unity的静态设置以及烘培属性
LeetCode·每日一题·814.二叉树剪枝·递归
Static setting and baking properties of unity
Cesium loads 3D tiles data
[development tutorial 7] crazy shell arm function mobile phone ble transparent transmission experiment tutorial
随机推荐
Leetcode · une question par jour · 814. Coupe d'arbre binaire · récursion
[chestnut sugar GIS] bat - how to rename the data in sub files in batch
Codeforces Round #807 (Div. 2)
微服务技术发展
Go language environment construction and samples
strcmp()&nbsp;- 比较字符串
Exclusive interview with sportlive Li Ying: how to use Web3 to promote sports IP to expand the "new business territory"
Brats18 - Multimodal MR image brain tumor segmentation challenge continued 5
GTSAM入门学习
ES6 从入门到精通 # 03:模板字符串
How to draw PCB outline machining drawing
2022.7.9暑假个人训练1-C . Clean up the Powers that Be
NFS共享存储服务
[chestnut sugar GIS] ArcMap - how to quickly generate the four directions information
MySQL data type
长期的远程工作面临的几个问题和持续改进的组织自动化
你离「TDengine 开发者大会」只差一条 SQL 语句!
Human moon day | exclusive interview with Zou Haiyang: China's aerospace dream is a story of trust and live up to
LeetCode-153-寻找旋转排序数组中的最小值
You are only one SQL statement away from the tdengine Developer Conference!