STM32CubeMX下环境搭建及编程烧录

本文介绍了如何安装STM32CubeMX和固件库,通过创建STM32工程并配置RCC、GPIO等设置,实现LED流水灯效果。在MDK-ARMV5环境下编写main函数,使用HAL库函数进行延时和管脚操作。最终通过调试器观察波形验证程序,并进行烧录实现硬件效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、STM32CubeMX的安装

2、固件库的安装

3、利用STM32CubeMX创建工程

4、填写main函数

5、烧写与硬件实现

六、总结

1、STM32CubeMX的安装

参考一下博客

https://blog.youkuaiyun.com/qq_43279579/article/details/11221319

2、固件库的安装

点击 Help -> Manage embedded software paclages 

选择最新的1.8.4,再点击install Now

下载完后关闭页面 

3、创建工程 

 

左上角有一个part number,我选的是STM32103C8

双击这个界面

 

回到system core点击RCC,在右边第一个界面选择Crystal/Ceramic Resona

下图从左到右依次修改,先选择HSE(第一个蓝点) ,再选择PLLCLK(第二个蓝点),最后将APB1 Prescaler改为/2

回到system core,点击SYS,将Debug改为serial Wire 

GPIO 设置,在右边图中找到 LED 灯对应引脚,选择 GPIO_Output,这里我选的PB5、PB5、PB7为LED的端口 

在GPIO output level将三个都改为high 

代码生成

在project manager下输入项目名和项目路径,选择应用的 IDE 开发环境 MDK-ARM V5 

 

点击GENERATE CODE生成 

 

填写main函数

打开文件进入main.c程序,

打开文件我们会发现main函数里只有一些初始化,并没有主要的程序,while(1)里面是空的,这时候需要我们来添加代码 

 下面添加三个流水灯交替闪烁的代码

 HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_5);//?PB5??????
		HAL_Delay(1000);
		HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_6);//?PB5??????
		HAL_Delay(1000);
		HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_7);//?PB5??????
		HAL_Delay(1000);

延时函数(HAL_Delay)
函数原型: void HAL_Delay (__IO uint32_t Delay)
参数说明:Delay表示所要延时的时间大小,单位是毫秒
写管脚函数(HAL_GPIO_WritePin)
函数原型:void HAL_GPIO_WritePin (GPIO_TypeDef * GPIOx, uint16_t
GPIO_Pin, GPIO_PinState PinState)
参数说明:
GPIOx: 管脚组(x的取值为A~G)
GPIO_Pin: 管脚编号(x的取值为0~15)
PinState: 管脚的状态(取值为GPIO_BIT_RESET表示0, GPIO_BIT_SET表示1)

点开debug将Dialog.DLL下改为DARMSTM.DLL,Parameter下改为-pSTM32F103C8

配置好后,点击Debug  

 

点击下图所示按键

 

点击左上角setup,弹出对话框,添加需要查看的波形的的引脚,点击右上角的添加输入

输入GPIOx_IDR.y

x为选择的GPIO口号

y为选择的引脚号。

 

 在VTREG界面输入dir vtreg

 将Command里的地址输入,同时在Display type选择bit

 

 如图则表明成功

 

五、烧录与硬件实现

点击开始编程

运行效果如图

 

 六、总结

本次实验总的来说要比上次用寄存器实现要简单,用STM32CubeMX来编程省了不少的时间。

SetupSTM32CubeMX-5.1.0 Introduction STM32CubeMX is a graphical tool for STM32 microcontrollers. It is part of the STM32Cube™ initiative (see Section 1) and is available either as a standalone application or as an Eclipse plug-in for integration in integrated development environments (IDEs). STM32CubeMX has the following key features: • Easy microcontroller selection covering the whole STM32 portfolio • Board selection from a list of STMicroelectronics boards • Easy microcontroller configuration (pins, clock tree, peripherals, middleware) and generation of the corresponding initialization C code • Easy switching to another microcontroller by importing a previously-saved configuration to a new MCU project • Easy exporting of current configuration to a compatible MCU • Generation of configuration reports • Generation of embedded C projects for a selection of integrated development environment tool chains. STM32CubeMX projects include the generated initialization C code, MISRA 2004 compliant STM32 HAL drivers, the middleware stacks required for the user configuration, and all the relevant files for opening and building the project in the selected IDE. • Power consumption calculation for a user-defined application sequence • Self-updates allowing the user to keep STM32CubeMX up-to-date • Download and update of STM32Cube embedded software required for user application development (see Appendix E for details on the STM32Cube embedded software offer) Although STM32CubeMX offers a user interface and generates C code compliant with STM32 MCU design and firmware solutions, users need to refer to the product technical documentation for details on actual implementations of microcontroller peripherals and firmware.
### STM32CubeMX与VSCode配置及烧录教程 #### 工具准备 为了实现基于STM32CubeMX和VSCode的开发环境搭建以及程序烧录,需先完成工具链的准备工作。这包括安装STM32CubeMX、VSCode及其插件、GNU Arm Embedded Toolchain编译器套件以及其他必要的依赖项[^1]。 #### 配置流程 以下是具体的配置过程: 1. **初始化项目** 使用STM32CubeMX创建一个新的工程文件并设置目标微控制器型号及相关外设参数。完成后导出适用于GCC编译器的Makefile项目到指定目录下。 2. **安装必要软件** - 安装最新版本的Visual Studio Code编辑器。 - 在VSCode中通过扩展市场添加C/C++ IntelliSense支持插件(由Microsoft提供),用于语法高亮显示等功能;同时还需要安装名为"Cortex-Debug"调试插件来处理ARM Cortex架构下的断点调试操作。 3. **配置工作区** 打开之前从STM32CubeMX导出来的源码所在路径作为新的VSCode workspace加载进来之后调整tasks.json以匹配make命令执行需求,并修改launch.json使得能够连接硬件设备进行在线调试模式启动。 4. **编写构建任务** 编辑`.vscode/tasks.json` 文件定义一个build task用来调用外部make utility完成整个项目的链接组装阶段转换成最终可执行映像文件(.elf): ```json { "version": "2.0.0", "tasks": [ { "label": "Build Project", "type": "shell", "command": "make all", "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } ``` 5. **设定调试选项** 修改 `.vscode/launch.json` 来适配实际使用的STLink v2 或其他兼容JTAG/SWD接口标准的编程器规格说明文档里提到的相关属性字段值比如 `request`, `type`, `name`, `preLaunchTask`, `miDebuggerPath`, `setupCommands` 等等: ```json { "version": "0.2.0", "configurations": [ { "name": "Cortex Debug", "type": "cortex-debug", "request": "launch", "servertype": "openocd", "executable": "./Output_Directory_Name/Project_Name.elf", "device": "STM32Fxxx", "interface": "swd", "configFiles": [ "board/st_nucleo_f4.cfg" ], "runToMain": true, "postRestartCommands": [ "break main" ] } ] } ``` 6. **上传固件至MCU** 当一切就绪后,在VSCode顶部菜单栏选择对应刚才建立好的configuration名称运行即可自动触发一系列动作直至成功把生成的目标代码灌入单片机内部flash存储区域当中去。 #### 注意事项 确保所有的驱动程序均已正确安装完毕并且物理连线无误(如SWDIO, SWCLK,GND,VCC),另外还需留意不同系列芯片可能存在的特殊要求或者差异之处。 ```bash st-flash write Build/Project_Name.elf 0x8000000 ``` 上述命令可以单独利用开源工具 st-link 实现快速刷写功能而不必每次都经历完整的gdb session握手协商环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值