STM32 开发 - stm32f10x.h 头文件(内存映射、寄存器结构体与宏、寄存器位定义、实现点灯案例)

概述

  • STM32F10x.h 是 STM32F1 系列微控制器的核心头文件,提供了所有外设寄存器的定义和内存映射

一、内存映射

c 复制代码
#define PERIPH_BASE           ((uint32_t)0x40000000)
c 复制代码
#define APB1PERIPH_BASE       PERIPH_BASE
#define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE        (PERIPH_BASE + 0x20000)
  1. PERIPH_BASE 是片上外设的起始地址,对于STM32系列是 0x40000000

  2. 根据这个基础地址,APB1、APB2、AHB 外设的地址空间是依次偏移的,分别为 +0x000000x100000x20000

c 复制代码
#define GPIOA_BASE            (APB2PERIPH_BASE + 0x0800)
#define GPIOB_BASE            (APB2PERIPH_BASE + 0x0C00)
#define GPIOC_BASE            (APB2PERIPH_BASE + 0x1000)
#define GPIOD_BASE            (APB2PERIPH_BASE + 0x1400)
#define GPIOE_BASE            (APB2PERIPH_BASE + 0x1800)
#define GPIOF_BASE            (APB2PERIPH_BASE + 0x1C00)
#define GPIOG_BASE            (APB2PERIPH_BASE + 0x2000)
  1. 访问 GPIO 外设的基地址定义,它们基于 APB2PERIPH_BASE

  2. GPIO 的地址按端口(GPIOA、GPIOB...)依次偏移 0x400


二、寄存器结构体与宏

1、复位和时钟控制 RCC
c 复制代码
typedef struct
{
  __IO uint32_t CR;			// 时钟控制寄存器
  __IO uint32_t CFGR;		// 时钟配置寄存器
  __IO uint32_t CIR;		// 时钟中断寄存器
  __IO uint32_t APB2RSTR;	// APB2 外设复位寄存器
  __IO uint32_t APB1RSTR;	// APB1 外设复位寄存器
  __IO uint32_t AHBENR;		// AHB 外设时钟使能寄存器
  __IO uint32_t APB2ENR;	// APB2 外设时钟使能寄存器
  __IO uint32_t APB1ENR;	// APB1 外设时钟使能寄存器
  __IO uint32_t BDCR;		// 备份域控制寄存器
  __IO uint32_t CSR;		// 控制 / 状态寄存器

#ifdef STM32F10X_CL  
  __IO uint32_t AHBRSTR;
  __IO uint32_t CFGR2;
#endif

#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)   
  uint32_t RESERVED0;
  __IO uint32_t CFGR2;
#endif
} RCC_TypeDef;
c 复制代码
#define RCC                 ((RCC_TypeDef *) RCC_BASE)
2、通用输入输出 GPIO
c 复制代码
typedef struct
{
  __IO uint32_t CRL;     // 端口配置低寄存器
  __IO uint32_t CRH;     // 端口配置高寄存器
  __IO uint32_t IDR;     // 输入数据寄存器
  __IO uint32_t ODR;     // 输出数据寄存器
  __IO uint32_t BSRR;    // 位设置/清除寄存器
  __IO uint32_t BRR;     // 位清除寄存器
  __IO uint32_t LCKR;    // 配置锁定寄存器
} GPIO_TypeDef;
c 复制代码
#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)
#define GPIOB               ((GPIO_TypeDef *) GPIOB_BASE)
#define GPIOC               ((GPIO_TypeDef *) GPIOC_BASE)
#define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE)
#define GPIOE               ((GPIO_TypeDef *) GPIOE_BASE)
#define GPIOF               ((GPIO_TypeDef *) GPIOF_BASE)
#define GPIOG               ((GPIO_TypeDef *) GPIOG_BASE)

三、寄存器位定义

1、RCC_APB2RSTR 寄存器位定义
c 复制代码
#define  RCC_APB2ENR_AFIOEN                  ((uint32_t)0x00000001)         /*!< Alternate Function I/O clock enable */
#define  RCC_APB2ENR_IOPAEN                  ((uint32_t)0x00000004)         /*!< I/O port A clock enable */
#define  RCC_APB2ENR_IOPBEN                  ((uint32_t)0x00000008)         /*!< I/O port B clock enable */
#define  RCC_APB2ENR_IOPCEN                  ((uint32_t)0x00000010)         /*!< I/O port C clock enable */
#define  RCC_APB2ENR_IOPDEN                  ((uint32_t)0x00000020)         /*!< I/O port D clock enable */
#define  RCC_APB2ENR_ADC1EN                  ((uint32_t)0x00000200)         /*!< ADC 1 interface clock enable */

#if !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD_VL)
#define  RCC_APB2ENR_ADC2EN                  ((uint32_t)0x00000400)         /*!< ADC 2 interface clock enable */
#endif

#define  RCC_APB2ENR_TIM1EN                  ((uint32_t)0x00000800)         /*!< TIM1 Timer clock enable */
#define  RCC_APB2ENR_SPI1EN                  ((uint32_t)0x00001000)         /*!< SPI 1 clock enable */
#define  RCC_APB2ENR_USART1EN                ((uint32_t)0x00004000)         /*!< USART1 clock enable */

#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
#define  RCC_APB2ENR_TIM15EN                 ((uint32_t)0x00010000)         /*!< TIM15 Timer clock enable */
#define  RCC_APB2ENR_TIM16EN                 ((uint32_t)0x00020000)         /*!< TIM16 Timer clock enable */
#define  RCC_APB2ENR_TIM17EN                 ((uint32_t)0x00040000)         /*!< TIM17 Timer clock enable */
#endif

#if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL)
 #define  RCC_APB2ENR_IOPEEN                 ((uint32_t)0x00000040)         /*!< I/O port E clock enable */
#endif /* STM32F10X_LD && STM32F10X_LD_VL */

#if defined (STM32F10X_HD) || defined (STM32F10X_XL)
 #define  RCC_APB2ENR_IOPFEN                 ((uint32_t)0x00000080)         /*!< I/O port F clock enable */
 #define  RCC_APB2ENR_IOPGEN                 ((uint32_t)0x00000100)         /*!< I/O port G clock enable */
 #define  RCC_APB2ENR_TIM8EN                 ((uint32_t)0x00002000)         /*!< TIM8 Timer clock enable */
 #define  RCC_APB2ENR_ADC3EN                 ((uint32_t)0x00008000)         /*!< DMA1 clock enable */
#endif

#if defined (STM32F10X_HD_VL)
 #define  RCC_APB2ENR_IOPFEN                 ((uint32_t)0x00000080)         /*!< I/O port F clock enable */
 #define  RCC_APB2ENR_IOPGEN                 ((uint32_t)0x00000100)         /*!< I/O port G clock enable */
#endif

#ifdef STM32F10X_XL
 #define  RCC_APB2ENR_TIM9EN                 ((uint32_t)0x00080000)         /*!< TIM9 Timer clock enable  */
 #define  RCC_APB2ENR_TIM10EN                ((uint32_t)0x00100000)         /*!< TIM10 Timer clock enable  */
 #define  RCC_APB2ENR_TIM11EN                ((uint32_t)0x00200000)         /*!< TIM11 Timer clock enable */
#endif
宏定义 说明
RCC_APB2ENR_AFIOE 0x00000001 复位 AFIO
RCC_APB2ENR_IOPAEN 0x00000004 使能 GPIOA
RCC_APB2ENR_IOPBEN 0x00000008 使能 GPIOB
RCC_APB2ENR_IOPCEN 0x00000010 使能 GPIOC
RCC_APB2ENR_IOPDEN 0x00000020 使能 GPIOD
RCC_APB2ENR_ADC1EN 0x00000200 使能 ADC1
RCC_APB2ENR_TIM1EN 0x00000800 使能高级定时器 TIM1
RCC_APB2ENR_SPI1EN 0x00001000 使能 SPI1
RCC_APB2ENR_USART1EN 0x00004000 使能 USART1
2、GPIO_CRL 寄存器位定义
c 复制代码
#define  GPIO_CRL_MODE                       ((uint32_t)0x33333333)        /*!< Port x mode bits */

#define  GPIO_CRL_MODE0                      ((uint32_t)0x00000003)        /*!< MODE0[1:0] bits (Port x mode bits, pin 0) */
#define  GPIO_CRL_MODE0_0                    ((uint32_t)0x00000001)        /*!< Bit 0 */
#define  GPIO_CRL_MODE0_1                    ((uint32_t)0x00000002)        /*!< Bit 1 */

#define  GPIO_CRL_MODE1                      ((uint32_t)0x00000030)        /*!< MODE1[1:0] bits (Port x mode bits, pin 1) */
#define  GPIO_CRL_MODE1_0                    ((uint32_t)0x00000010)        /*!< Bit 0 */
#define  GPIO_CRL_MODE1_1                    ((uint32_t)0x00000020)        /*!< Bit 1 */

#define  GPIO_CRL_MODE2                      ((uint32_t)0x00000300)        /*!< MODE2[1:0] bits (Port x mode bits, pin 2) */
#define  GPIO_CRL_MODE2_0                    ((uint32_t)0x00000100)        /*!< Bit 0 */
#define  GPIO_CRL_MODE2_1                    ((uint32_t)0x00000200)        /*!< Bit 1 */

#define  GPIO_CRL_MODE3                      ((uint32_t)0x00003000)        /*!< MODE3[1:0] bits (Port x mode bits, pin 3) */
#define  GPIO_CRL_MODE3_0                    ((uint32_t)0x00001000)        /*!< Bit 0 */
#define  GPIO_CRL_MODE3_1                    ((uint32_t)0x00002000)        /*!< Bit 1 */

#define  GPIO_CRL_MODE4                      ((uint32_t)0x00030000)        /*!< MODE4[1:0] bits (Port x mode bits, pin 4) */
#define  GPIO_CRL_MODE4_0                    ((uint32_t)0x00010000)        /*!< Bit 0 */
#define  GPIO_CRL_MODE4_1                    ((uint32_t)0x00020000)        /*!< Bit 1 */

#define  GPIO_CRL_MODE5                      ((uint32_t)0x00300000)        /*!< MODE5[1:0] bits (Port x mode bits, pin 5) */
#define  GPIO_CRL_MODE5_0                    ((uint32_t)0x00100000)        /*!< Bit 0 */
#define  GPIO_CRL_MODE5_1                    ((uint32_t)0x00200000)        /*!< Bit 1 */

#define  GPIO_CRL_MODE6                      ((uint32_t)0x03000000)        /*!< MODE6[1:0] bits (Port x mode bits, pin 6) */
#define  GPIO_CRL_MODE6_0                    ((uint32_t)0x01000000)        /*!< Bit 0 */
#define  GPIO_CRL_MODE6_1                    ((uint32_t)0x02000000)        /*!< Bit 1 */

#define  GPIO_CRL_MODE7                      ((uint32_t)0x30000000)        /*!< MODE7[1:0] bits (Port x mode bits, pin 7) */
#define  GPIO_CRL_MODE7_0                    ((uint32_t)0x10000000)        /*!< Bit 0 */
#define  GPIO_CRL_MODE7_1                    ((uint32_t)0x20000000)        /*!< Bit 1 */

#define  GPIO_CRL_CNF                        ((uint32_t)0xCCCCCCCC)        /*!< Port x configuration bits */

#define  GPIO_CRL_CNF0                       ((uint32_t)0x0000000C)        /*!< CNF0[1:0] bits (Port x configuration bits, pin 0) */
#define  GPIO_CRL_CNF0_0                     ((uint32_t)0x00000004)        /*!< Bit 0 */
#define  GPIO_CRL_CNF0_1                     ((uint32_t)0x00000008)        /*!< Bit 1 */

#define  GPIO_CRL_CNF1                       ((uint32_t)0x000000C0)        /*!< CNF1[1:0] bits (Port x configuration bits, pin 1) */
#define  GPIO_CRL_CNF1_0                     ((uint32_t)0x00000040)        /*!< Bit 0 */
#define  GPIO_CRL_CNF1_1                     ((uint32_t)0x00000080)        /*!< Bit 1 */

#define  GPIO_CRL_CNF2                       ((uint32_t)0x00000C00)        /*!< CNF2[1:0] bits (Port x configuration bits, pin 2) */
#define  GPIO_CRL_CNF2_0                     ((uint32_t)0x00000400)        /*!< Bit 0 */
#define  GPIO_CRL_CNF2_1                     ((uint32_t)0x00000800)        /*!< Bit 1 */

#define  GPIO_CRL_CNF3                       ((uint32_t)0x0000C000)        /*!< CNF3[1:0] bits (Port x configuration bits, pin 3) */
#define  GPIO_CRL_CNF3_0                     ((uint32_t)0x00004000)        /*!< Bit 0 */
#define  GPIO_CRL_CNF3_1                     ((uint32_t)0x00008000)        /*!< Bit 1 */

#define  GPIO_CRL_CNF4                       ((uint32_t)0x000C0000)        /*!< CNF4[1:0] bits (Port x configuration bits, pin 4) */
#define  GPIO_CRL_CNF4_0                     ((uint32_t)0x00040000)        /*!< Bit 0 */
#define  GPIO_CRL_CNF4_1                     ((uint32_t)0x00080000)        /*!< Bit 1 */

#define  GPIO_CRL_CNF5                       ((uint32_t)0x00C00000)        /*!< CNF5[1:0] bits (Port x configuration bits, pin 5) */
#define  GPIO_CRL_CNF5_0                     ((uint32_t)0x00400000)        /*!< Bit 0 */
#define  GPIO_CRL_CNF5_1                     ((uint32_t)0x00800000)        /*!< Bit 1 */

#define  GPIO_CRL_CNF6                       ((uint32_t)0x0C000000)        /*!< CNF6[1:0] bits (Port x configuration bits, pin 6) */
#define  GPIO_CRL_CNF6_0                     ((uint32_t)0x04000000)        /*!< Bit 0 */
#define  GPIO_CRL_CNF6_1                     ((uint32_t)0x08000000)        /*!< Bit 1 */

#define  GPIO_CRL_CNF7                       ((uint32_t)0xC0000000)        /*!< CNF7[1:0] bits (Port x configuration bits, pin 7) */
#define  GPIO_CRL_CNF7_0                     ((uint32_t)0x40000000)        /*!< Bit 0 */
#define  GPIO_CRL_CNF7_1                     ((uint32_t)0x80000000)        /*!< Bit 1 */
宏定义 值(十六进制) 值(二进制) 功能描述
GPIO_CRL_MODE 0x33333333 ... 00110011 所有引脚的 MODE 位掩码(复位值)
GPIO_CRL_MODE0 0x00000003 ... 00000011 引脚 0 的 MODE 位 0、1
GPIO_CRL_MODE0_0 0x00000001 ... 00000001 引脚 0 的 MODE 位 0
GPIO_CRL_MODE0_1 0x00000002 ... 00000010 引脚 0 的 MODE 位 1
GPIO_CRL_MODE1 0x00000030 ... 00110000 引脚 1 的 MODE 位 4 、5
GPIO_CRL_MODE1_0 0x00000010 ... 00010000 引脚 1 的 MODE 位 4
GPIO_CRL_MODE1_1 0x00000020 ... 00100000 引脚 1 的 MODE 为 5
... ... ... ...
宏定义 值(十六进制) 值(二进制) 功能描述
GPIO_CRL_CNF 0xCCCCCCCC ... 11001100 所有引脚的 CNF 位掩码(复位值)
GPIO_CRL_CNF0 0x0000000C ... 00001100 引脚 0 的 CNF 位 2、3
GPIO_CRL_CNF0_0 0x00000004 ... 00000100 引脚 0 的 CNF 位 2
GPIO_CRL_CNF0_1 0x00000008 ... 00001000 引脚 0 的 CNF 位 3
GPIO_CRL_CNF1 0x000000C0 ... 11000000 引脚 1 的 CNF 位 6、7
GPIO_CRL_CNF1_0 0x00000040 ... 01000000 引脚 1 的 CNF 位 6
GPIO_CRL_CNF1_1 0x00000080 ... 10000000 引脚 1 的 CNF 位 7
... ... ... ...
3、GPIO_ODR 寄存器位定义
c 复制代码
#define GPIO_ODR_ODR0                        ((uint16_t)0x0001)            /*!< Port output data, bit 0 */
#define GPIO_ODR_ODR1                        ((uint16_t)0x0002)            /*!< Port output data, bit 1 */
#define GPIO_ODR_ODR2                        ((uint16_t)0x0004)            /*!< Port output data, bit 2 */
#define GPIO_ODR_ODR3                        ((uint16_t)0x0008)            /*!< Port output data, bit 3 */
#define GPIO_ODR_ODR4                        ((uint16_t)0x0010)            /*!< Port output data, bit 4 */
#define GPIO_ODR_ODR5                        ((uint16_t)0x0020)            /*!< Port output data, bit 5 */
#define GPIO_ODR_ODR6                        ((uint16_t)0x0040)            /*!< Port output data, bit 6 */
#define GPIO_ODR_ODR7                        ((uint16_t)0x0080)            /*!< Port output data, bit 7 */
#define GPIO_ODR_ODR8                        ((uint16_t)0x0100)            /*!< Port output data, bit 8 */
#define GPIO_ODR_ODR9                        ((uint16_t)0x0200)            /*!< Port output data, bit 9 */
#define GPIO_ODR_ODR10                       ((uint16_t)0x0400)            /*!< Port output data, bit 10 */
#define GPIO_ODR_ODR11                       ((uint16_t)0x0800)            /*!< Port output data, bit 11 */
#define GPIO_ODR_ODR12                       ((uint16_t)0x1000)            /*!< Port output data, bit 12 */
#define GPIO_ODR_ODR13                       ((uint16_t)0x2000)            /*!< Port output data, bit 13 */
#define GPIO_ODR_ODR14                       ((uint16_t)0x4000)            /*!< Port output data, bit 14 */
#define GPIO_ODR_ODR15                       ((uint16_t)0x8000)            /*!< Port output data, bit 15 */
宏定义 值(十六进制) 二进制值 说明
GPIO_ODR_ODR0 0x0001 ... 00000001 设置引脚 0
GPIO_ODR_ODR1 0x0002 ... 00000010 设置引脚 1
GPIO_ODR_ODR2 0x0004 ... 00000100 设置引脚 2
GPIO_ODR_ODR3 0x0008 ... 00001000 设置引脚 3
... ... ... ...

实现点灯案例

c 复制代码
#include "stm32f10x.h"

int main()
{
    // RCC APB2 使能
    RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;

    // 清除 PB5 的配置位 [23:20]
    // GPIO_CRL_MODE5 为 00000000 00110000 00000000 00000000
    // GPIO_CRL_CNF5 为 00000000 11000000 00000000 00000000
    // GPIO_CRL_MODE5 | GPIO_CRL_CNF5 得到 00000000 11110000 00000000 00000000
    // ~(GPIO_CRL_MODE5 | GPIO_CRL_CNF5) 得到 11111111 00001111 11111111 11111111
    GPIOB->CRL &= ~(GPIO_CRL_MODE5 | GPIO_CRL_CNF5);

    // 设置 PB5 为推挽输出模式,最大速度为 50MH
    // GPIO_CRL_MODE5_0 为 00000000 00010000 00000000 00000000
    // GPIO_CRL_MODE5_1 为 00000000 00100000 00000000 00000000
    // GPIO_CRL_MODE5_0 | GPIO_CRL_MODE5_1 得到 00000000 00110000 00000000 00000000
    GPIOB->CRL |= GPIO_CRL_MODE5_0 | GPIO_CRL_MODE5_1;

    // 点亮 PB5
    // GPIO_ODR_ODR5 为 00000000 00000000 00000000 00010000
    // ~GPIO_ODR_ODR5 得到 11111111 11111111 11111111 11101111
    GPIOB->ODR &= ~GPIO_ODR_ODR5;

    while (1)
    {
    }
}
相关推荐
enyp8010 分钟前
Qt文本文件读写方法详解
开发语言·c++·算法
我的golang之路果然有问题16 分钟前
快速了解Go+微服务(概念和一个例子)
开发语言·笔记·后端·学习·微服务·golang
福理原乡大王1 小时前
进程地址空间
java·开发语言·算法
OG one.Z1 小时前
文件读取操作
c++·学习·文件读取
CodeWithMe2 小时前
【中间件】bthread效率为什么高?
开发语言·中间件
青苔猿猿2 小时前
(4)python中jupyter lab使用python虚拟环境
开发语言·python·jupyter·虚拟环境
彷徨而立2 小时前
【C++】频繁分配和释放会产生内存碎片
开发语言·c++
code_shenbing2 小时前
C# 实现列式存储数据
开发语言·c#·存储
Bt年2 小时前
第十六届蓝桥杯 C/C++ B组 题解
c语言·c++·蓝桥杯
Kairo_013 小时前
在 API 模拟阶段:Apipost vs. Faker.js vs. Postman —— 为什么 Apipost 是最优选择
开发语言·javascript·postman