如何使用Visual Studio Code+STM32Cube MX优雅的编写STM32程序(3)

(在Windows10操作系统上,基于STM32CubeMX arm-gcc和Openocd)

​ 在上一篇文稿当中我们学会了如何用Visual Studio Code+STM32Cube MX在现有工程框架增加自己的代码,整体即不破坏原框架又让代码 整洁美观。

​ 下面就以跑马灯的案例基础上增加Printf函数重定向,及浮点数的打印说明

一:Printf函数重定向

MY_Printf.c文件新增重定向函数:

在MyApplication文件下新增MyPrintf.c,编写如下代码

特别说明:

1.因huart1指向串口函数,需要在"MyApplication.h"下新增#include "usart.h"

2.在"MyPrintf.c"下需新增#include "MyApplication.h"

3.在"MyApplication.mk"下需要新增"MyApplication/MyPrintf.c "

#include "MyApplication.h"
/* Private function prototypes -----------------------------------------------*/
//使用Printf一定要加\n

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) 
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xF

你可能感兴趣的:(STM32学习笔记,vscode)