## 代码规划 生成代码的时候 ,选择 code generat 选项 自动根据外设生成 .h/.c 就自动分配好文件了 ### config.h 项目基本配置文件,引入 stm32f1xx_hal.h, 部分定义正在这个文件,如 uint16_t port pin 的定义 ```c #ifndef __CONFIG_H #define __CONFIG_H #include "stm32f1xx_hal.h" typedef uint32_t u32; typedef uint16_t u16; typedef uint8_t u8; // led port pin #define DS1_Pin GPIO_PIN_5 #define DS1_GPIO_Port GPIOE #define DS0_Pin GPIO_PIN_5 #define DS0_GPIO_Port GPIOB // uart #endif ``` ### 外设 每个外设写一个 .h .c 如: led.h led.c 包含 以数据的形式封装 ### #define __HAL_RCC_GPIOA_CLK_ENABLE() do { \ __IO uint32_t tmpreg = 0x00U; \ SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);\ /* Delay after an RCC peripheral clock enabling */ \ tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);\ UNUSED(tmpreg); \ } while(0U) 设置寄存器RCC->AHB1ENR的相关位为1 使能端口时钟 stm32f4xx_hal_rcc.h头文件中定义 __HAL_RCC_DMA1_CLK_ENABLE();//使能 DMA1 时钟 __HAL_RCC_USART2_CLK_ENABLE();//使能串口 2 时钟 __HAL_RCC_TIM1_CLK_ENABLE();//使能 TIM1 时钟