STM32CubeMX 6.14安装配置与Keil工程生成避坑指南STM32CubeMX 6.14 Installation, Configuration and Keil Project Generation: A Pitfall-Avoidance Guide


AI free token/AI免费积分

STM32CubeMX 6.14安装配置与Keil工程生成避坑指南

本文来源:http://www.lpqz.cn/news/180729
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

STM32CubeMX这个工具做嵌入式的兄弟应该都不陌生。最近我把手上几个项目都切到了 6.14 版本顺手把从官网下载、安装、芯片包导入、新建工程到生成 Keil 工程的全流程重新捋了一遍发现网上教程要么太老要么只讲一半特别是 6.x 系列之后的界面和固件包管理逻辑跟老版本差别不小。今天这篇就把我从零开始的操作细节、踩过的坑、以及一些反直觉的小问题一次性说清楚内容偏实操新手可以照着一步步来老手也能当个速查手册。1. 正式配置之前先把 CubeMX 的定位说清楚1.1 为什么现在写代码几乎离不开它简单说STM32CubeMX 是意法半导体官方出的图形化配置工具你通过点鼠标把引脚、时钟、外设参数设好它直接给你生成一套基于 HAL 库或者 LL 库的 C 工程代码。十年前写 STM32 那可是寄存器裸奔配一个 USART 要翻几百页参考手册算波特率算到头大现在这些事在 CubeMX 里就是几秒钟

的事。它的核心价值在于三点一是把芯片成千上万的引脚复用关系图形化同一个引脚能不能做 USART2_TX界面上直接给你标清楚避免对着数据手册发呆二是时钟树自动解算你只要告诉它“我要系统跑 72MHz”它会自动算出 PLL 的参数算不通直接报错三是生成代码和用户代码分离只要把业务逻辑写在 USER CODE 标记区里下次改配置重新生成代码你的逻辑还在。1.2 什么人适合看这篇如果你是刚接触 STM32 的新手CubeMX 能帮你把“配置芯片”这件事的成本降到最低把更多精力放在业务逻辑上。如果你已经在用寄存器或者标准库开发我也建议至少把 CubeMX 生成的初始化代码当做一个参考基准对照一下自己的初始化是否漏了某个关键的 RCC 时钟使能或者 GPIO 速度设置。不过要提个醒CubeMX 不是万能的。它生成的是“能用”的初始化代码但不一定是最优的。比如某些低功耗场景、时序要求极高的外设驱动还是得

手动调寄存器。所以正确的心态是把它当成脚手架而不是免死金牌。2. 版本 6.14 的下载与安装全过程2.1 下载渠道和账号准备STM32CubeMX 的下载渠道其实只有一个官方标准答案ST 官网。打开 st.com 后搜索 STM32CubeMX找到 Tools Software 栏目下的这个工具。别去第三方下载站原因有二一是非官方渠道的安装包可能有被篡改的风险二是你后面下芯片固件包、获取技术支持都需要 ST 账号索性一开始就注册一个。注册账号没啥门槛邮箱收个验证码就行。有一点经验是尽量用常用邮箱因为固件包下载记录、许可证这些东西都跟账号绑定换了邮箱有时候授权要重新弄。下载页面会让你选择操作系统版本Windows 用户选 Windows InstallerLinux 用户有 .zip 包macOS 用户选对应 package 即可。6.14 的安装包体积大概在 500MB 上下注意看下载

文件的完整性我遇到过几次浏览器断点续传导致压缩包损坏的情况表现是双击安装包直接报错“不是有效的 Win32 应用程序”这时候别怀疑系统重新完整下载一遍基本能解决。2.2 安装流程与首次启动的坑安装过程本身没什么好说的一路 Next 即可。但我强烈建议做两件事一是把安装路径改到纯英文、无空格的目录比如D:\STM32CubeMX中文路径和带空格路径在后续生成工程、调用编译器时会有一堆莫名其妙的问题二是在安装类型上选择“Install for all users”如果你用的是公司电脑当前用户权限不够装到用户目录后面写固件库时容易报权限错误。首次启动时 CubeMX 会弹出一个对话框问你要不要检查更新我建议先选 No因为刚装完 6.14 你直接连服务器更新很有可能会遇到下载缓慢或者连接超时的情况我们先把本地环境跑通后面再处理更新不迟。首次启动还会要求你选择工作空间目录这个目录是用来放工程和固件包

的同样建议放在一个纯英文路径下比如D:\STM32CubeMX\workspace。启动之后主界面相对简洁左边是一系列快捷入口包括新建工程、芯片选择器、固件包管理等。到这一步安装就算完成了但别急着新建工程下一步先解决芯片支持包的问题。3. 芯片包管理让固件库不再是拦路虎3.1 固件包到底是什么很多新手第一次打开 CubeMX 新建工程时会懵明明安装了软件为什么选完芯片型号后提示需要下载固件库这个固件库在 ST 的术语里叫 STM32Cube Firmware Package它是某个芯片系列完整的 HAL 库源码、驱动中间件比如 FATFS、USB、LWIP 等、例程和文档的集合。比如你选了 STM32F103C8T6CubeMX 就需要 STM32Cube FW_F1 这个包没有它就无法生成工程。固件包可以从线上下载也可以手动从官网下载 Zip 包再导入。这个机制是固件包与 CubeMX

主程序解耦设计的好处是主程序更新不用重下所有芯片库坏处就是国内网络环境下在线下载经常失败。3.2 在线安装失败的常见原因在 CubeMX 的Help - Manage embedded software packages里可以看到已安装和可用的固件包列表。勾选某个系列点击 Install理论上它会从 ST 的服务器下载并自动解压到本地仓库。实际使用中我遇到的大多数失败都属于这三种情况第一种是网络问题下载进度条长时间不动或者直接弹窗报错这跟服务器连通性有关ST 的服务器在国外高峰期真的很难连上。第二种是权限问题如果安装 CubeMX 时选了当前用户安装而固件仓库目录在 Program Files 下写入时会报 access denied。第三种是版本冲突本地已经有一个版本的固件包新版本安装失败后残留了不完整的数据导致后续怎么装都不行。解决在线安装失败最直接有效的办法就是手动导入也正好应对了热

搜词里“cube firmware cannot be installed into repository”这个典型报错。别跟在线安装死磕我实测手动方式成功率接近百分之百。3.3 手动导入固件包的正确姿势手动导入的完整操作大概是这样的。先到 ST 官网搜对应系列的名字比如 STM32CubeF1进入页面后选择 STM32CubeF1或者对应系列的固件包下载 Zip 格式注意看版本号尽量和 CubeMX 要求的版本一致下载时也要登录账号。打开 CubeMX进入Help - Manage embedded software packages点击左下角的From Local...按钮选中你刚下载的 Zip 文件CubeMX 会自动解析并安装。这个操作对网络中断特别有效装完之后到Installed列表里确认版本号变成绿色即可。我特别想提醒的是固件包解压后不要手动去改里面的任何文件有些教程让它去修改

某些初始化配置这个操作在旧版工程里可能有效但 6.14 生成的代码是重新读取固件库源码的你手工改掉的文件在下一次重新生成工程时会丢失你本地的修改而且很难排查。需要定制 HAL 代码的合理做法是直接改你工程里Drivers/STM32xx_HAL_Driver/Src下的对应文件这个后面再细说。4. 新建工程与核心外设配置全流程4.1 用芯片选择器找到你要的那颗 MCU固件包就绪后点击主界面的Access to MCU Selector进入芯片选择器。这里有几个筛选维度系列Series、内核Core、封装Package、Flash 容量等。我一般建议直接用左上角的搜索框输入具体的芯片型号比如 STM32F103C8T6能最快定位。选中型号后右侧会有芯片的基本信息包括 Flash 和 RAM 大小、最大主频、封装引脚数等。点击Start Project进入图形化配置界面。如果是老工程升级可以直

接用File - Load Project加载原来的 .ioc 文件。这里有一个小习惯我建工程时会先在电脑上建好项目文件夹把 .ioc 文件、MDK 工程等归类而不是让 CubeMX 把一堆文件散落在默认目录里。4.2 时钟树配置里的门道时钟树是新手最容易翻车的地方。打开配置界面后左侧是外设列表右侧是芯片引脚图底部通常是时钟树配置页。以最常见的 STM32F103C8T6 为例想让系统跑 72MHz 最高主频必须这么设RCC 时钟源往里看HSE高速外部时钟选择Crystal/Ceramic Resonator也就是板子上那个 8MHz 晶振系统时钟源System Clock Source选择PLLCLK然后在 PLL 配置里PLL 倍数PLL Mul设为 9这样 8MHz x 9 72MHz。时钟树图上会实时显示各个总线的频率如果某个外设的时钟超过了允许的最大值会显示红色并报错。这里有个

高频翻车点APB1 总线最大允许频率是 36MHzAPB2 是 72MHz很多人把 APB1 预分频器设成 1 然后看到 72MHz觉得没问题实际上如果下方挂着 USART3 之类的 APB1 外设就有隐患所以官方推荐的配置是 APB1 分频 /2得到 36MHzAPB2 分频 /1保持 72MHz。每次改完时钟都能在左上角看到当前系统时钟 Synchro 的频率一定要确认它是你要的值再往下走。对于带以太网或 USB 的芯片比如 F4、H7 系列还要特别注意 USB 外设必须跑在精确的 48MHz不是 72MHz 平分出来的这时候时钟树上会有一个专门的“48MHz 时钟源”选项通常是 PRTCLK 或者 PLL48CLK忘了勾选的话 USB 枚举会失败这是我们常说的“时钟树少一条线”问题。4.3 GPIO 与常用外设配置实操时钟树搞定后配置外设就比较直观了。在左侧 Categories 列

表里点击GPIO右侧芯片图上的每个引脚都可以点击切换模式。比如要把 PA5 设为 LED 驱动引脚直接左键点击 PA5 选择GPIO_Output然后在下方 GPIO 配置里把输出速度设为High、初始电平设为High这样上电灯就亮、标签命名为LED0。USART 配置也很常用。在左侧点USART1模式选择Asynchronous异步模式右边会出现一堆配置项。波特率一般设为 115200字长 8 位无校验1 个停止位这是串口助手的默认配置。如果要用中断接收打开NVIC Settings勾选 USART1 global interrupt如果要用 DMA 发送打开DMA Settings添加 USART1_TX 通道模式选 Normal传输数据宽度选 Byte。这样生成的代码里会初始化 DMA 并开启了收发功能比自己写 DMA 配置省事太多。SPI 的配置类似模式选Full-Duplex M

aster硬件 NSS 信号如果不用就直接禁掉用 GPIO 软件控制片选更灵活。I2C 就一个坑不同板子上拉电阻的情况不同速率设 100kHz 还是比较稳的400kHz 快速模式对走线和上拉要求比较高容易出问题。定时器 PWM 输出配置时注意先选PWM Generation CHx然后在Parameter Settings里设置 Prescaler预分频和 Counter Period自动重装载值这两个值跟 PWM 频率的关系是频率 定时器时钟 / (PSC 1) / (ARR 1)。比如要产生 1kHz 的 PWM时钟 72MHz一般设 PSC 71ARR 999输出就是 1kHz占空比由后面代码里的 CCR 值决定。4.4 中断、DMA 与项目设置细节在NVIC里能统一管理所有中断优先级。如果你用的是一个外设就勾一个中断CubeMX 默认的优先级分组是 4 位抢占优先级对大多

数项目够用。但记住一个原则中断服务函数里别做耗时的操作只置标志位主循环里处理业务逻辑这是嵌入式开发的铁律跟用什么工具生成代码无关。项目设置这里特别容易出问题。打开Project Manager标签页设置 Project Name 和 Location我再次强调路径里不能有中文和空格。Toolchain / IDE 选择MDK-ARM V5.27或者 V6如果你装了 Keil5选 V5 版本基本兼容如果用的是 CLion 或者 VSCode GCC选STM32CubeIDE或Makefile。底下Code Generator栏有几个选项要注意Generate peripheral initialization as a pair of .c/.h files per peripheral这条勾选后每个外设单独生成一个 .c/.h 文件而不是全部塞进 main.c我强烈建议勾选代码整洁度提升

一个档次Backup previously generated files when re-generating建议不勾不然重新生成时会出现一堆 .bak 备份文件干扰阅读Copy only the necessary library files勾选后只拷贝用到的库文件减少工程体积。5. 生成代码后实际操作的关键网点5.1 生成的代码长什么样点击右上角Generate Code按钮CubeMX 会在指定目录下创建 Keil/MDK 工程目录结构。包括Core目录存放 main.c、中断处理文件、系统时钟配置等、Drivers目录CMSIS 和 HAL 驱动源码、.mxproject文件。其中 main.c 是主入口核心函数如MX_GPIO_Init、MX_USART1_UART_Init都是自动生成的每个初始化函数上方都有/* USER CODE BEGIN */注释标记。第一次打开生成的

代码我建议不要急着写主循环先手过一遍这些初始化函数。看它们是否按照时钟树配置里设定的参数正确初始化 PCKL、USART、GPIO 等。这个过程非常能加深理解相当于免费的“标准答案”摆在面前。5.2 用户代码怎么加才不会被覆盖这是 CubeMX 使用中最核心的一个规律所有你自己写的代码一定要放在 USER CODE 标记区之间。比如你要在系统初始化完、主循环开始前加一段打印应该这样写int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN WHILE */ printf(System boot OK\r\n); /* USER CODE END WHILE */ while (1) { /* USER CODE BEGIN 1 */

STM32CubeMX 6.14 Installation, Configuration and Keil Project Generation: A Pitfall-Avoidance Guide

本文来源:http://www.lpqz.cn/news/180729
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Anyone doing embedded work should be familiar with STM32CubeMX. Recently I switched several of my projects to version 6.14 and took the opportunity to re-walk the whole flow — downloading from the official site, installation, importing chip packs, creating a project, and generating a Keil project. I found that online tutorials are either too old or only tell half the story, especially since the 6.x series, whose interface and firmware-pack management logic differ quite a bit from older versions. This article lays out, in one go, the from-scratch operation details, the pitfalls I hit, and a few counter-intuitive little problems. It's hands-on: beginners can follow it step by step, and veterans can use it as a quick-reference manual. 1. Before the real configuration: getting CubeMX's role straight 1.1 Why you can barely write code without it today Simply put, STM32CubeMX is STMicroelectronics' official graphical configuration tool: you click your way through setting pins, clocks, and peripheral parameters, and it generates a complete C project based on the HAL or LL library. Writing STM32 code ten years ago meant running bare-metal on registers — configuring one USART meant flipping through hundreds of pages of the reference manual and calculating baud rates until your head hurt. Now all of that takes a few seconds

in CubeMX. Its core value comes in three parts. First, it visualizes the thousands of pin-multiplexing relationships of a chip: whether the same pin can serve as USART2_TX is spelled out right in the UI, so you don't sit staring at the datasheet. Second, it solves the clock tree automatically: tell it "I want the system at 72 MHz" and it computes the PLL parameters, erroring out immediately if it can't. Third, generated code is separated from user code: as long as you write your business logic inside the USER CODE markers, your logic survives the next regenerate after a configuration change. 1.2 Who this article is for If you're new to STM32, CubeMX lowers the cost of "configuring the chip" to a minimum so you can focus more energy on business logic. If you already develop with registers or the standard library, I'd still suggest using the init code CubeMX generates as a reference baseline and checking your own initialization against it for a missed RCC clock enable or GPIO speed setting. But a warning: CubeMX isn't a cure-all. The initialization code it generates is "working" code, not necessarily optimal. For some low-power scenarios, or peripheral drivers with very demanding timing, you still need to

tweak registers by hand. So the right mindset is to treat it as scaffolding, not a get-out-of-jail-free card. 2. Downloading and installing version 6.14, start to finish 2.1 Download channels and account prep There's really only one official answer for downloading STM32CubeMX: the ST website. Open st.com, search for STM32CubeMX, and find the tool under the Tools Software section. Don't use third-party download sites, for two reasons: installers from unofficial channels risk being tampered with, and you'll need an ST account later to download chip firmware packs and get technical support — so just register one up front. There's no barrier to signing up; verify your email with a code and you're in. One tip: use a frequently used email, because firmware download records and licenses are tied to the account, and switching emails can mean redoing authorization. The download page asks you to pick an OS version: Windows users choose Windows Installer, Linux users get a .zip package, macOS users pick the corresponding package. The 6.14 installer is roughly 500 MB, so watch the download

file's integrity. I've run into cases where a browser resume corrupted the archive, showing up as a double-click error of "not a valid Win32 application." When that happens, don't blame the system — just re-download it completely and that usually fixes it. 2.2 Installation flow and first-launch pitfalls The installation itself is unremarkable — just click Next all the way. But I strongly recommend two things. First, change the install path to an all-English, space-free directory such as D:\STM32CubeMX; Chinese paths and paths with spaces cause all sorts of mysterious problems later when generating projects and invoking the compiler. Second, on the install-type screen choose "Install for all users" — if you're on a company computer with insufficient privileges for the current user, installing into the user directory tends to produce permission errors when writing firmware libraries later. On first launch CubeMX pops up a dialog asking whether to check for updates; I suggest choosing No, because right after installing 6.14 a direct server update is likely to be slow or time out. Get the local environment working first, then deal with updates. First launch also asks you to choose a workspace directory, which is where projects and firmware packs

go — again, put it under an all-English path such as D:\STM32CubeMX\workspace. After launch, the main UI is fairly clean; on the left are shortcuts including New Project, MCU Selector, and Firmware Package Management. At this point installation is done, but don't rush to create a project — first deal with the chip support pack. 3. Chip-pack management: making firmware libraries no longer a roadblock 3.1 What exactly is a firmware pack? Many beginners are baffled the first time they open CubeMX to create a project: the software is clearly installed, so why does it prompt to download a firmware library after selecting a chip? In ST's terminology this library is the STM32Cube Firmware Package — the complete HAL source, driver middleware (FATFS, USB, LWIP, etc.), examples, and documentation for a given chip family. Pick an STM32F103C8T6, for instance, and CubeMX needs the STM32Cube FW_F1 pack; without it, no project can be generated. Firmware packs can be downloaded online, or fetched manually from the website as a Zip and imported. This mechanism decouples the firmware pack from the CubeMX

main program. The upside is that updating the main program doesn't force you to re-download every chip library; the downside is that online downloads often fail on domestic networks. 3.2 Common causes of online install failures In CubeMX's Help - Manage embedded software packages you can see the list of installed and available firmware packs. Check a series and click Install; in theory it downloads from ST's servers and auto-extracts into the local repository. In practice, most failures I've hit fall into three categories. First, network problems: the progress bar stalls for ages or an error dialog pops up — this is about server connectivity, and ST's servers, being overseas, are genuinely hard to reach at peak times. Second, permissions: if you chose a current-user install for CubeMX but the firmware repository sits under Program Files, writes will fail with access denied. Third, version conflicts: an older version is already present, a failed new-version install leaves incomplete data behind, and nothing you try afterwards works. The most direct and effective fix for online install failures is manual import — which also happens to answer the hot

search term "cube firmware cannot be installed into repository," a classic error. Don't wrestle with online installation — in my tests the manual approach succeeds close to 100% of the time. 3.3 The right way to import a firmware pack manually The full manual-import procedure goes roughly like this. First, search the ST site for the corresponding series name, e.g. STM32CubeF1; on the page, choose the STM32CubeF1 or relevant series firmware pack and download the Zip format, paying attention to the version number — try to match what CubeMX requires — and log in with your account when downloading. Open CubeMX, go to Help - Manage embedded software packages, click the From Local... button at the lower left, pick the Zip you just downloaded, and CubeMX will parse and install it automatically. This is especially effective against network interruptions; afterwards check the Installed list and confirm the version number has turned green. One thing I want to stress: after extraction, don't manually modify any files inside the pack. Some tutorials tell you to modify

certain initialization settings. That may have worked in older projects, but the code generated by 6.14 re-reads the firmware library source, so your hand-edited files will lose your local changes on the next regenerate, and that's hard to diagnose. The sensible way to customize HAL code is to edit the corresponding files directly under Drivers/STM32xx_HAL_Driver/Src in your project — more on that later. 4. Creating a project and configuring core peripherals, end to end 4.1 Finding your MCU with the chip selector Once the firmware pack is ready, click Access to MCU Selector on the main screen to enter the chip selector. There are several filter dimensions: Series, Core, Package, Flash size, and so on. I usually recommend typing the exact part number in the search box at the upper left — e.g. STM32F103C8T6 — for the fastest hit. After selecting a part, the right side shows basic info including Flash and RAM size, max frequency, and package pin count. Click Start Project to enter the graphical configuration UI. If you're upgrading an older project, you can directly

use File - Load Project to load the original .ioc file. A small habit of mine: before creating a project I make a project folder on disk and keep the .ioc file, MDK project, and related files together, instead of letting CubeMX scatter a bunch of files in the default directory. 4.2 The tricks of clock-tree configuration The clock tree is where beginners most often crash. Once the configuration UI opens, the left side is the peripheral list, the right side the chip pinout, and the bottom is usually the clock-tree configuration tab. Taking the most common STM32F103C8T6 as an example, to run the system at the 72 MHz maximum you must set it up like this: under RCC clock source, for HSE (high-speed external clock) select Crystal/Ceramic Resonator — the 8 MHz crystal on the board; for System Clock Source choose PLLCLK; then in the PLL configuration set the PLL multiplier (PLL Mul) to 9, giving 8 MHz × 9 = 72 MHz. The clock-tree diagram shows each bus frequency in real time; if a peripheral's clock exceeds the allowed maximum, it turns red and raises an error. Here's a

frequent crash point: the APB1 bus allows at most 36 MHz while APB2 allows 72 MHz. Many people set the APB1 prescaler to 1, see 72 MHz, and think it's fine — but if an APB1 peripheral like USART3 hangs off it, you have a latent problem. So the officially recommended setup is APB1 divided by 2 to get 36 MHz, and APB2 divided by 1 to keep 72 MHz. Every time you change the clocks, check the current system clock (Synchro) frequency shown at the upper left and make sure it's the value you want before moving on. For chips with Ethernet or USB — the F4 and H7 series, say — another detail matters: the USB peripheral must run at exactly 48 MHz, not a divided-down 72 MHz. The clock tree then has a dedicated "48 MHz clock source" option, usually PRTCLK or PLL48CLK; forget to enable it and USB enumeration fails — the classic "one missing wire in the clock tree" problem. 4.3 Configuring GPIO and common peripherals in practice With the clock tree sorted, peripheral configuration is fairly intuitive. In the left-hand Categories list

click GPIO; every pin on the chip diagram to the right can be clicked to change its mode. For example, to make PA5 drive an LED, just left-click PA5, choose GPIO_Output, then in the GPIO settings below set the output speed to High and the initial level to High so the LED lights at power-on, and name the label LED0. USART configuration is another common one. Click USART1 on the left, choose Asynchronous mode, and a bunch of options appear on the right. Set the baud rate to 115200, word length 8 bits, no parity, 1 stop bit — the serial assistant's defaults. For interrupt reception, open NVIC Settings and enable USART1 global interrupt; for DMA transmission, open DMA Settings, add the USART1_TX channel, set mode to Normal and data width to Byte. The generated code will then initialize DMA and enable transmit/receive, far less trouble than writing DMA config yourself. SPI configuration is similar: choose Full-Duplex M

aster; if you don't use the hardware NSS signal, just disable it — controlling chip select in software via GPIO is more flexible. I2C has one pitfall: pull-up resistors differ from board to board, so 100 kHz is the safer rate; 400 kHz fast mode is demanding about trace routing and pull-ups and tends to cause trouble. For timer PWM output, first select PWM Generation CHx, then in Parameter Settings set the Prescaler and Counter Period (auto-reload value). Their relation to PWM frequency is: frequency = timer clock / (PSC + 1) / (ARR + 1). For example, to generate 1 kHz PWM with a 72 MHz clock, typically set PSC = 71 and ARR = 999 for a 1 kHz output; the duty cycle is determined by the CCR value in code later. 4.4 Interrupt, DMA, and project-setting details The NVIC lets you manage all interrupt priorities in one place. If you're enabling one interrupt per peripheral, CubeMX's default priority grouping of 4 bits of preemption priority is fine for most

projects. But remember one principle: don't do time-consuming work inside an ISR — just set a flag and handle the business logic in the main loop. That's an iron rule of embedded development, regardless of which tool generated the code. Project settings are a particularly common source of trouble. Open the Project Manager tab and set Project Name and Location — again, no Chinese characters or spaces in the path. For Toolchain / IDE choose MDK-ARM V5.27 or V6; if you have Keil 5 installed, the V5 option is basically compatible. If you use CLion or VSCode + GCC, choose STM32CubeIDE or Makefile. A few options in the Code Generator section below deserve attention. "Generate peripheral initialization as a pair of .c/.h files per peripheral" — when checked, each peripheral gets its own .c/.h file instead of everything being dumped into main.c; I strongly recommend enabling it, as code tidiness improves

by a notch. "Backup previously generated files when re-generating" — I suggest leaving it off, otherwise regeneration spits out a pile of .bak backup files that clutter reading. "Copy only the necessary library files" — check it to copy only the libraries actually used and shrink the project. 5. Key points after code generation 5.1 What the generated code looks like Click Generate Code in the upper right and CubeMX creates the Keil/MDK project directory structure under the chosen location: a Core directory (main.c, interrupt handlers, system clock config, etc.), a Drivers directory (CMSIS and HAL driver sources), and a .mxproject file. main.c is the main entry; core functions such as MX_GPIO_Init and MX_USART1_UART_Init are all auto-generated, each with a /* USER CODE BEGIN */ comment marker above it. The first time you open the generated

code, I suggest not rushing to write the main loop; first walk through those initialization functions by hand. Check whether they initialized PCKL, USART, GPIO, etc. correctly according to the parameters set in the clock tree. This process really deepens your understanding — it's like having a free "answer key" in front of you. 5.2 How to add user code without it being overwritten This is the single most important rule in using CubeMX: all code you write yourself must go between the USER CODE markers. For example, to print a message after system initialization and before the main loop, write it like this: int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN WHILE */ printf(System boot OK\r\n); /* USER CODE END WHILE */ while (1) { /* USER CODE BEGIN 1 */

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry