当前位置: 首页 > news >正文

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

概述

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

一、内存映射

#define PERIPH_BASE           ((uint32_t)0x40000000)
#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

#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
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;
#define RCC                 ((RCC_TypeDef *) RCC_BASE)
2、通用输入输出 GPIO
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;
#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 寄存器位定义
#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_AFIOE0x00000001复位 AFIO
RCC_APB2ENR_IOPAEN0x00000004使能 GPIOA
RCC_APB2ENR_IOPBEN0x00000008使能 GPIOB
RCC_APB2ENR_IOPCEN0x00000010使能 GPIOC
RCC_APB2ENR_IOPDEN0x00000020使能 GPIOD
RCC_APB2ENR_ADC1EN0x00000200使能 ADC1
RCC_APB2ENR_TIM1EN0x00000800使能高级定时器 TIM1
RCC_APB2ENR_SPI1EN0x00001000使能 SPI1
RCC_APB2ENR_USART1EN0x00004000使能 USART1
2、GPIO_CRL 寄存器位定义
#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_MODE0x33333333... 00110011所有引脚的 MODE 位掩码(复位值)
GPIO_CRL_MODE00x00000003... 00000011引脚 0 的 MODE 位 0、1
GPIO_CRL_MODE0_00x00000001... 00000001引脚 0 的 MODE 位 0
GPIO_CRL_MODE0_10x00000002... 00000010引脚 0 的 MODE 位 1
GPIO_CRL_MODE10x00000030... 00110000引脚 1 的 MODE 位 4 、5
GPIO_CRL_MODE1_00x00000010... 00010000引脚 1 的 MODE 位 4
GPIO_CRL_MODE1_10x00000020... 00100000引脚 1 的 MODE 为 5
宏定义值(十六进制)值(二进制)功能描述
GPIO_CRL_CNF0xCCCCCCCC... 11001100所有引脚的 CNF 位掩码(复位值)
GPIO_CRL_CNF00x0000000C... 00001100引脚 0 的 CNF 位 2、3
GPIO_CRL_CNF0_00x00000004... 00000100引脚 0 的 CNF 位 2
GPIO_CRL_CNF0_10x00000008... 00001000引脚 0 的 CNF 位 3
GPIO_CRL_CNF10x000000C0... 11000000引脚 1 的 CNF 位 6、7
GPIO_CRL_CNF1_00x00000040... 01000000引脚 1 的 CNF 位 6
GPIO_CRL_CNF1_10x00000080... 10000000引脚 1 的 CNF 位 7
3、GPIO_ODR 寄存器位定义
#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_ODR00x0001... 00000001设置引脚 0
GPIO_ODR_ODR10x0002... 00000010设置引脚 1
GPIO_ODR_ODR20x0004... 00000100设置引脚 2
GPIO_ODR_ODR30x0008... 00001000设置引脚 3

实现点灯案例

#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 11111111GPIOB->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 00000000GPIOB->CRL |= GPIO_CRL_MODE5_0 | GPIO_CRL_MODE5_1;// 点亮 PB5// GPIO_ODR_ODR5 为 00000000 00000000 00000000 00010000// ~GPIO_ODR_ODR5 得到 11111111 11111111 11111111 11101111GPIOB->ODR &= ~GPIO_ODR_ODR5;while (1){}
}

相关文章:

  • DAM-3B,英伟达推出的多模态大语言模型
  • 正确应对监管部门的数据安全审查
  • KEPServerEX 6与西门子1500PLC进行OPC通讯
  • 搜广推校招面经八十二
  • 代码随想录打卡|Day28 动态规划(理论基础、斐波那契数列、爬楼梯、使用最小花费爬楼梯)
  • Pycharm 代理配置
  • 【C】初阶数据结构13 -- 快速排序
  • 【Pandas】pandas DataFrame rmul
  • IP数据报发送和转发的过程
  • c语言知识整理
  • LLaMa Factory大模型微调
  • 机器学习——朴素贝叶斯法运用
  • 小白如何学会完整挪用Github项目?(以pix2pix为例)
  • Android Compose 框架矢量图标深入剖析(七)
  • compose 二维码扫描qrcode
  • Swift与iOS内存管理机制深度剖析
  • 【随笔】地理探测器原理与运用
  • 剑指offer经典题目(六)
  • OTA和IAP的关系
  • TI---UART通信
  • 游戏论|迟来的忍者与武士:从《刺客信条:影》论多元话语的争议
  • 重新认识中国女性|婚姻,自古以来就是一桩生意
  • 因商标近似李小龙形象被裁定无效,真功夫起诉国家知产局,法院判了
  • 涉军民事案件类型日益增多,最高法新规明晰管辖争议问题
  • 潘功胜:央行将实施好适度宽松的货币政策,推动中国经济高质量发展
  • 《哪吒之魔童降世》电影版权方诉《仙侠神域》游戏运营方侵权案开庭