-u_printf_float
1.点击truestudio,新建->其他->系统调用,然后将syscalls.c选择路径为src下即可。
2.在uart.c下添加如下语句
/* USER CODE BEGIN 0 */
#include "stdio.h"
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
PUTCHAR_PROTOTYPE
{
// 注意下面第一个参数是&huart1,因为cubemx配置了串口1自动生成的
HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif
/* USER CODE END 0 */
3.在syscalls.c文件下,修改函数 int _write(int32_t file, uint8_t *ptr, int32_t len)
int _write(int32_t file, uint8_t *ptr, int32_t len)
{
/* Implement your write code here, this is used by puts and printf for example */
int DataIdx;
for (DataIdx = 0; DataIdx < len;DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
/* return len; */
errno = ENOSYS;
return -1;
}
4、就此便可直接通过printf输出数据但是没法输出浮点数,我们可以进行如下设置即可完美支持浮点数的输出。在工程属性下找到C/C++ build->Settings->Tool Settings->C Linker->Miscellaneous->Other options 选项空中填写:-u_printf_float即可。到此为止TrueStudio即可支持printf的所有数据类型输出。
5.最好能加上
新建->其他-库函数-微小printf实现