stm32f103c8t6 结构体
时间: 2025-05-17 19:13:37 浏览: 17
### STM32F103C8T6 结构体定义与使用
STM32F103C8T6 是一款基于 ARM Cortex-M3 内核的微控制器,其硬件资源通过寄存器映射到内存空间。为了简化开发流程并提高代码可读性,ST 提供了两种主要的软件库支持:标准外设库 (Standard Peripheral Library) 和 HAL 库 (Hardware Abstraction Layer)。
#### 1. 标准外设库中的结构体定义
在标准外设库中,所有的外设都被抽象成特定的数据结构。这些数据结构通常位于头文件 `stm32f1xx.h` 中,并且每个外设有对应的初始化结构体用于配置功能。例如:
- **GPIO 初始化结构体**
```c
typedef struct {
uint32_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
uint32_t GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIO_mode_define */
uint32_t GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIO_speed_define */
} GPIO_InitTypeDef;
```
上述结构体用于配置 GPIO 的引脚模式、速度等属性[^2]。
- **定时器初始化结构体**
对于通用定时器 TIMx,标准外设库提供了类似的初始化结构体:
```c
typedef struct {
uint16_t TIM_Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock.
This parameter must be a number between 0x0000 and 0xFFFF. */
uint16_t TIM_CounterMode; /*!< Specifies the counter mode.
This parameter can be a value of @ref TIM_Counter_Mode */
uint16_t TIM_Period; /*!< Specifies the period value to be loaded into the active auto-reload register.
This parameter must be a number between 0x0000 and 0xFFFF. */
uint16_t TIM_ClockDivision; /*!< Specifies the clock division.
This parameter can be a value of @ref TIM_Clock_Division */
} TIM_TimeBaseInitTypeDef;
```
该结构体允许开发者设置预分频器值、计数模式、自动重装载值以及时钟分割因子等参数[^4]。
#### 2. HAL 库中的结构体定义
HAL 库进一步封装了底层细节,提供更高层次的 API 接口。以下是 HAL 库中常见的几个结构体及其用途:
- **GPIO 初始化结构体**
```c
typedef struct {
uint32_t Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any combination of the @ref GPIO_pins_definitions values */
uint32_t Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIO_mode_values */
uint32_t Pull; /*!< Specifies the pulling resistance for the selected pins.
This parameter can be a value of @ref GPIO_pull_resistor_values */
uint32_t Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIO_speed_values */
uint32_t Alternate; /*!< Peripheral to be connected to the selected pins.
This parameter can be a value of @ref GPIO_Alternate_function_selection */
} GPIO_InitTypeDef;
```
此结构体的功能类似于标准外设库中的对应部分,但增加了对上下拉电阻的支持和更灵活的复用功能选项[^1]。
- **TIM 基础时间基线初始化结构体**
```c
typedef struct {
uint32_t Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock.
This parameter can be a number between 0x0000 and 0xFFFF */
uint32_t CounterMode; /*!< Specifies the counter mode.
This parameter can be a value of @ref TIM_Counter_Mode */
uint32_t Period; /*!< Specifies the period value to be loaded into the active auto-reload register.
This parameter must be a number between 0x0000 and 0xFFFFFFFF */
uint32_t ClockDivision; /*!< Specifies the clock division.
This parameter can be a value of @ref TIM_Clock_Division */
uint32_t AutoReloadPreload; /*!< Specifies whether the TIM's registers are buffered.
This parameter can be a value of @ref TIM_Auto_Reload_Preload_Definitions */
} TIM_Base_InitTypeDef;
```
相比标准外设库,HAL 库的时间基线初始化结构体新增了一个字段 `AutoReloadPreload`,用于指定是否启用缓冲机制。
#### 3. 使用方法对比
无论是标准外设库还是 HAL 库,在实际应用中都需要先实例化相应的初始化结构体,再调用初始化函数完成外设配置。例如:
##### 标准外设库示例
```c
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
```
##### HAL 库示例
```c
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
```
尽管两者语法略有不同,但基本逻辑一致:填充结构体成员变量 -> 调用初始化函数。
---
阅读全文
相关推荐


















