GCC STM32 串口重定向

GCC STM32 串口重定向

#include "stdio.h"

int _write(int fd, char *pBuffer, int size)
{
    while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) == 0);
    HAL_UART_Transmit(&huart1, (uint8_t *)pBuffer, size, 0xff);
    return size;
}

可重定向,使用printf,但是无法输出float格式

参考他人博客修改makefile编译失败


#Makefile中添加
    -u _printf_float
 
例:# libraries
    LIBS = -lc -lm -lnosys 
    LIBDIR = 
    LDFLAGS = $(MCU) -specs=nano.specs -u _printf_float -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

打印float

float转string然后printf。示例如下,不选择sprintf,因为它也没法用。

#include <stdio.h>
#include <stdlib.h>

int main() {
    float num = 123.456;
    char str[20];

    gcvt(num, 6, str);

    printf("The float number as a string: %s\n", str);

    return 0;
}

gcvt函数的第一个参数是要转换的浮点数,第二个参数是要保留的小数位数,第三个参数是结果字符串。只能暂时这样可以打印float数据。

[参考]
https://blog.csdn.net/for_dream1205/article/details/101556546
https://www.cnblogs.com/Luad/p/12635132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值