生活随笔
收集整理的這篇文章主要介紹了
STM32F4的GPIO使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.GPIO時鐘使能 void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState);
參數RCC_AHB1Periph可選擇的范圍如下:
#define RCC_AHB1Periph_GPIOA ((uint32_t)0x00000001)
#define RCC_AHB1Periph_GPIOB ((uint32_t)0x00000002)
#define RCC_AHB1Periph_GPIOC ((uint32_t)0x00000004)
#define RCC_AHB1Periph_GPIOD ((uint32_t)0x00000008)
#define RCC_AHB1Periph_GPIOE ((uint32_t)0x00000010)
#define RCC_AHB1Periph_GPIOF ((uint32_t)0x00000020)
#define RCC_AHB1Periph_GPIOG ((uint32_t)0x00000040)
#define RCC_AHB1Periph_GPIOH ((uint32_t)0x00000080)
#define RCC_AHB1Periph_GPIOI ((uint32_t)0x00000100)
#define RCC_AHB1Periph_GPIOJ ((uint32_t)0x00000200)
#define RCC_AHB1Periph_GPIOK ((uint32_t)0x00000400)
typedef enum {
DISABLE
= 0 ,
ENABLE
= ! DISABLE
} FunctionalState
;
if ( NewState
!= DISABLE
) { RCC
-> AHB1ENR
| = RCC_AHB1Periph
; } else { RCC
-> AHB1ENR
& = ~ RCC_AHB1Periph
; }
通過改寫外設時鐘使能寄存器(RCC_AHB1ENR) 使能和去使能外設
RCC的定義 就是RCC_TypeDef結構體的地址 RCC的邊界地址為:0x4002 3800 - 0x4002 3BFF(大小0x3FF) 所以RCC_BASE就是0x4002 3800
#define RCC ((RCC_TypeDef *) RCC_BASE)
typedef struct
{ __IO uint32_t CR
; __IO uint32_t PLLCFGR
; __IO uint32_t CFGR
; __IO uint32_t CIR
; __IO uint32_t AHB1RSTR
; __IO uint32_t AHB2RSTR
; __IO uint32_t AHB3RSTR
; uint32_t RESERVED0
; __IO uint32_t APB1RSTR
; __IO uint32_t APB2RSTR
; uint32_t RESERVED1
[ 2 ] ; __IO uint32_t AHB1ENR
; __IO uint32_t AHB2ENR
; __IO uint32_t AHB3ENR
; uint32_t RESERVED2
; __IO uint32_t APB1ENR
; __IO uint32_t APB2ENR
; uint32_t RESERVED3
[ 2 ] ; __IO uint32_t AHB1LPENR
; __IO uint32_t AHB2LPENR
; __IO uint32_t AHB3LPENR
; uint32_t RESERVED4
; __IO uint32_t APB1LPENR
; __IO uint32_t APB2LPENR
; uint32_t RESERVED5
[ 2 ] ; __IO uint32_t BDCR
; __IO uint32_t CSR
; uint32_t RESERVED6
[ 2 ] ; __IO uint32_t SSCGR
; __IO uint32_t PLLI2SCFGR
; __IO uint32_t PLLSAICFGR
; __IO uint32_t DCKCFGR
; } RCC_TypeDef
;
(可以看出來結構體大小4乘32個byte,明顯小于0x3FF)
二.GPIO初始化 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
typedef struct
{ __IO uint32_t MODER
; __IO uint32_t OTYPER
; __IO uint32_t OSPEEDR
; __IO uint32_t PUPDR
; __IO uint32_t IDR
; __IO uint32_t ODR
; __IO uint16_t BSRRL
; __IO uint16_t BSRRH
; __IO uint32_t LCKR
; __IO uint32_t AFR
[ 2 ] ;
} 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)
#define GPIOH ((GPIO_TypeDef *) GPIOH_BASE)
#define GPIOI ((GPIO_TypeDef *) GPIOI_BASE)
#define GPIOJ ((GPIO_TypeDef *) GPIOJ_BASE)
#define GPIOK ((GPIO_TypeDef *) GPIOK_BASE)
邊界地址圖(似乎沒有JK,但是和GPIOI和CRC間留了0x800的位置)
結構體GPIO_InitTypeDef 用于設置IO口,以及端口模式寄存器 (GPIOx_MODER)、端口輸出類型寄存器 (GPIOx_OTYPER)、端口輸出速度寄存器 (GPIOx_OSPEEDR)、端口上拉/下拉寄存器 (GPIOx_PUPDR)
typedef struct
{ uint32_t GPIO_Pin
; GPIOMode_TypeDef GPIO_Mode
; GPIOSpeed_TypeDef GPIO_Speed
; GPIOOType_TypeDef GPIO_OType
; GPIOPuPd_TypeDef GPIO_PuPd
;
} GPIO_InitTypeDef
;
#define GPIO_Pin_0 ((uint16_t)0x0001)
#define GPIO_Pin_1 ((uint16_t)0x0002)
#define GPIO_Pin_2 ((uint16_t)0x0004)
#define GPIO_Pin_3 ((uint16_t)0x0008)
#define GPIO_Pin_4 ((uint16_t)0x0010)
#define GPIO_Pin_5 ((uint16_t)0x0020)
#define GPIO_Pin_6 ((uint16_t)0x0040)
#define GPIO_Pin_7 ((uint16_t)0x0080)
#define GPIO_Pin_8 ((uint16_t)0x0100)
#define GPIO_Pin_9 ((uint16_t)0x0200)
#define GPIO_Pin_10 ((uint16_t)0x0400)
#define GPIO_Pin_11 ((uint16_t)0x0800)
#define GPIO_Pin_12 ((uint16_t)0x1000)
#define GPIO_Pin_13 ((uint16_t)0x2000)
#define GPIO_Pin_14 ((uint16_t)0x4000)
#define GPIO_Pin_15 ((uint16_t)0x8000)
#define GPIO_Pin_All ((uint16_t)0xFFFF)
GPIO_Mode的選擇范圍 用于端口模式寄存器 (GPIOx_MODER)的配置 分別是輸入(IN)、輸出(OUT)、復用(AF)和模擬(AN) 其中復用就是,使用IO的特殊功能usart、spi等等 模擬就是采集和輸出模擬量用的ADC、DAC
typedef enum
{ GPIO_Mode_IN
= 0x00 , GPIO_Mode_OUT
= 0x01 , GPIO_Mode_AF
= 0x02 , GPIO_Mode_AN
= 0x03
} GPIOMode_TypeDef
;
32位的寄存器每個IO口的模式配置占用兩位
GPIO_Speed的選擇范圍 用于端口輸出類型寄存器 (GPIOx_OTYPER) 的配置 分別是推挽(PP)、開漏(OD) 推挽可以正常輸出高低電平,開漏要輸出高電平需要外部上拉電阻
typedef enum
{ GPIO_OType_PP
= 0x00 , GPIO_OType_OD
= 0x01
} GPIOOType_TypeDef
;
32位的寄存器前面16位給每個IO口配置輸出類型,后面16位預留
GPIO_Speed的選擇范圍 用于端口輸出速度寄存器 (GPIOx_OSPEEDR)的配置 分別是2 MHz(低速)、:25 MHz(中速)、50 MHz(快速) 以及100 MHz(高速)
typedef enum
{ GPIO_Low_Speed
= 0x00 , GPIO_Medium_Speed
= 0x01 , GPIO_Fast_Speed
= 0x02 , GPIO_High_Speed
= 0x03
} GPIOSpeed_TypeDef
;
32位的寄存器每個IO口的速度配置占用兩位
GPIO_PuPd的選擇范圍 用于端口上拉/下拉寄存器 (GPIOx_PUPDR)的配置 分別是無上拉下拉、上拉、下拉,還有一個保留
typedef enum
{ GPIO_PuPd_NOPULL
= 0x00 , GPIO_PuPd_UP
= 0x01 , GPIO_PuPd_DOWN
= 0x02
} GPIOPuPd_TypeDef
;
32位的寄存器每個IO口的上下拉配置占用兩位
輸出和復用模式下的配置表: 輸入和模擬模式下的配置表:
總結
以上是生活随笔 為你收集整理的STM32F4的GPIO使用 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。