当前位置:网站首页>double型数据转字符串后通过MCU串口发送
double型数据转字符串后通过MCU串口发送
2022-08-03 05:24:00 【langaopeng】
double型数据转字符串如下函数,函数的功能如:将 double=12.123的数据转为CString = "12.123";后通过串口发出
void PutDataTOChar(double dat)
{
u8 i;
u8 tab[10];//存整数部分
u8 bot[6];//存小数部分
u8 iIndex =0;//整数部分指针
u8 jIndex =0;//小数部分指针
int aa = 0;
double pot = 0.0;
for(i=0;i<10;i++)
{
tab[i] = ' ';
bot[i] = ' ';
}
aa = (int) dat;//取整数
if(aa < 0) //是负数//
{
dat = -dat;
aa = -aa;
pot = dat-aa;
while(aa/10 > 0)//将int 型转为字符型
{
tab[iIndex++] = aa%10; // 取出个位
aa /= 10;
}
tab[iIndex] = aa;
for(i=0;i<5;i++)//将小数部分转为字符型
{
bot[jIndex++] = (int)(pot*10);
pot = pot*10 - (int)(pot*10);
}
putchar('-');
for(i=iIndex;i>0;i--)
{
putchar(tab[i]+0x30);
}
putchar(tab[i]+0x30);
putchar('.');
for(i=0;i<jIndex;i++)
{
putchar(bot[i]+0x30);
}
}
else
{
while(aa/10 > 0)
{
tab[iIndex++] = aa%10; // 取出个位
aa /= 10;
}
tab[iIndex] = aa;
for(i=0;i<5;i++)
{
bot[jIndex++] = (int)(pot*10);
pot = pot*10 - (int)(pot*10);
}
for(i=iIndex;i>0;i--)
{
putchar(tab[i]+0x30);
}
putchar(tab[i]+0x30);
putchar('.');
for(i=0;i<jIndex;i++)
{
putchar(bot[i]+0x30);
}
}
}
边栏推荐
猜你喜欢

嵌入式实验四

【DC-5 Range Penetration】

EIP-5058 能否防止NFT项目方提桶跑路?

The ` monorepo ` ` hoist ` mechanism lead to the change of the loading configuration file path

Ansible installation and deployment detailed process, basic operation of configuration inventory

自监督论文阅读笔记 DenseCL:Dense Contrastive Learning for Self-Supervised Visual Pre-Training

交叉熵(第六周)

Makefile

设备树解析源码分析<devicetree>-1.基础结构
深度学习理论课程第八、九、十章总结
随机推荐
深度学习理论课程第八、九、十章总结
代码没写完,哪里有脸睡觉!17 张程序员壁纸推荐
Ansible installation and deployment detailed process, basic operation of configuration inventory
[Rebound shell and privilege escalation]
opencv
边缘辅助无人机网络的分层联邦学习
理论上的嵌入式跑马灯
布尔盲注需简化代码
二阶段提问总结
中国食品微生物检测行业深度监测及投资战略规划建议报告2022~2028年
中国柔性制造系统(FMS)市场发展动态及未来趋势预测报告2022~2028年
[CSRF, SSRF, XXE, PHP deserialization, Burpsuite]
【解读合约审计】Harmony的跨链桥是如何被盗一亿美金的?
mysql 客户端SSL错误2026 (HY000)
A.1#【内存管理】——1.1.2 zone: struct zone
A.1#【内存管理】——1.1.4 node: 初始化
The ` monorepo ` ` hoist ` mechanism lead to the change of the loading configuration file path
对象の使用
VSCODE 常见问题
神经网络基础