【STM32】使用STM32CubeMX生成工程文件详细教程

00. 目录

文章目录

  • 00. 目录
  • 01. STM32CubeMX概述
  • 02. STM32CubeMX工程目录
  • 03. MDK-ARM工程文件介绍
  • 04. 主程序详解
  • 05. 附录
  • 01. STM32CubeMX概述

    STM32CubeMX是STM32Cube工具家族中的一员,从MCU/MPU选型,引脚配置,系统时钟以及外设时钟设置,到外设参数配置,中间件参数配置,它给STM32开发者们提供了一种简单,方便,并且直观的方式来完成这些工作。所有的配置完成后,它还可以根据所选的IDE生成对应的工程和初始化C代码。除此以外,STM32CubeMX还提供了功耗计算工具,可作为产品设计中功耗评估的参考。

    02. STM32CubeMX工程目录

    03. MDK-ARM工程文件介绍

    startup_stm32f103xe.s

    针对 STM32F103xE 系列微控制器的启动文件。它包含了微控制器启动时执行的低级初始化代码,比如设置向量表和初始化数据。

    Application/User/Core:

    用户代码目录

    main.c:

    这是程序的入口点,包含 main 函数。主要实现程序逻辑

    gpio.c:

    管理和实现与通用输入/输出(GPIO)引脚相关的功能。

    stm32f1xx_it.c:

    包含中断服务例程(ISR)。在这里处理所有的中断请求。

    stm32f1xx_hal_msp.c:

    MSP代表“MCU Specific Package”。这个文件包含了所有HAL库要求用户定义的回调函数,如用于初始化硬件(如GPIO、DMA、ADC等)的低级初始化函数。

    Drivers/STM32F1xx_HAL_Driver:

    这个目录包含了STM32F1系列的HAL(硬件抽象层)驱动代码,用于简化硬件接口的编程。

    Drivers/CMSIS:

    CMSIS代表“Cortex Microcontroller Software Interface Standard”,是ARM提供的一个硬件抽象层,旨在保持与ARM Cortex处理器兼容的代码的一致性。这个目录包含了CMSIS相关的核心文件。

    后续主要在Application/User/Core文件中进行操作

    04. 主程序详解

    main.c

    /* USER CODE BEGIN Header */
    /**
      * @file           : main.c
      * @brief          : Main program body
      ******************************************************************************
      * @attention
      *
      * Copyright (c) 2023 STMicroelectronics.
      * All rights reserved.
      *
      * This software is licensed under terms that can be found in the LICENSE file
      * in the root directory of this software component.
      * If no LICENSE file comes with this software, it is provided AS-IS.
      *
      ******************************************************************************
      */
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    // 这里添加的私有包含
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    // 这里添加私有类型定义
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    // 这里添加私有宏定义
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    // 这里添加私有宏
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    /* USER CODE BEGIN PV */
    // 这里添加私有变量
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    // 这里添加私有函数原型
    /* USER CODE END PFP */
    
    /* USER CODE BEGIN 0 */
    // 这里添加自定义代码
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    int main(void)
    {
      /* USER CODE BEGIN 1 */
      // 初始化代码
      /* USER CODE END 1 */
    
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
        /* USER CODE BEGIN Init */
      // 用户自定义的初始化
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
      // 系统初始化代码
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
    
      /* USER CODE BEGIN 2 */
      // 这里添加程序代码
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE BEGIN 3 */
        // 主循环
      }
      /* USER CODE END 3 */
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      // 系统时钟配置代码
    }
    
    /* USER CODE BEGIN 4 */
    // 这里添加更多的用户代码
    /* USER CODE END 4 */
    
    /**
      * @brief  This function is executed in case of error occurrence.
      * @retval None
      */
    void Error_Handler(void)
    {
      /* USER CODE BEGIN Error_Handler_Debug */
      // 错误处理代码
      /* USER CODE END Error_Handler_Debug */
    }
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *         where the assert_param error has occurred.
      * @param  file: pointer to the source file name
      * @param  line: assert_param error line source number
      * @retval None
      */
    void assert_failed(uint8_t *file, uint32_t line)
    {
      /* USER CODE BEGIN 6 */
      // 断言失败处理
      /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */
    
    

    05. 附录

    作者:沧海一笑-dj

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【STM32】使用STM32CubeMX生成工程文件详细教程

    发表回复