Featured image of post 项目构建

项目构建

项目构建

搭建框架

这个项目使用的STM32CubeMX + HAL 库进行搭建的

  • 首先STM32CubeMX搭建最小系统,选型单片机的类型
  • 其次设置烧录方式 + 时钟

image-20260409233138996

image-20260409233158548

image-20260409233211956

日志打印

通过串口实现日志打印

这里使用的STM32的串口3,UART3_TX PB10和UART3_RX PB11

配置UART3

image-20260409233622115

其他的模式选项

image-20260409233858220

模式 核心特点 接线
Disable 关闭外设
Asynchronous 异步 UART,全双工 TX/RX 2 根线
Synchronous 同步 USART,高速 TX/RX/CK 3 根线
Single Wire (Half-Duplex) 单线半双工,省 IO 1 根线
Multiprocessor Communication 多机主从通信 1/2 根线
IrDA 红外无线通信 红外模块
LIN 汽车 LIN 总线 1 根线
SmartCard 智能卡通信 多线
  • 创建一个common的文件夹存放自己写的程序
  • 新建C0m_debug.c、C0m_debug.h文件

C0m_debug.c

1
2
3
4
5
6
7
8
9
#include <Com_debug.h>

//重定向fputc函数到串口
int fputc(int ch, FILE *f)
{
    //将字符发送到串口
    HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 1000);
    return ch;
}

C0m_debug.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#ifndef COM_DEBUG_H
#define COM_DEBUG_H

#include "usart.h"
#include "stdio.h"
#include "stdarg.h"

//设置一个日志输出打印开关
#define DEBUG_LOG_ENABLE 1

#ifdef DEBUG_LOG_ENABLE
//使用宏定义,只打印文件名和行号
#define debug_printf(format,...)  printf("[%s:%d]  " format, __FILE__, __LINE__,##__VA_ARGS__)

#else
//如果不开启日志输出,则定义为空    
#define debug_printf(format,...)
#endif //ifdef

#endif

为什么要设置开关,因为日志输出打印在CPU运行上非常占用资源

通过比特率计算 打印10字节大概需要1ms,影响飞机的飞行,所以飞行时,关闭打印

最后更新于 2026-04-11 23:53
...