這個(gè)是百度搜集的一個(gè)STM32的GPIO口設(shè)置風(fēng)格,剛?cè)腴T的萌新們拿走不謝 1、首先定義GPIO的初始化類型結(jié)構(gòu)體:GPIO_InitTypeDefGPIO_InitStructure;此結(jié)構(gòu)體的定義是在stm32f10x_gpio.h文件中,其中包括3個(gè)成員。 1. /** 2. * @brief GPIO Init structure definition 3. */ 4. 5. typedef struct 6. { 7. uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured. 8. This parameter can be any value of @ref GPIO_pins_define */ 9. 10. GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins. 11. This parameter can be a value of @ref GPIOSpeed_TypeDef */ 12. 13. GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins. 14. This parameter can be a value of @ref GPIOMode_TypeDef */ 15. }GPIO_InitTypeDef;
(1)uint16_t GPIO_Pin;來指定GPIO的哪個(gè)或哪些引腳,取值參見本頭文件的宏定義,可以同時(shí)指定一個(gè)或多個(gè)要配置的引腳; 1. /** @defgroup GPIO_pins_define 2. * @{ 3. */ 4. 5. #define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */ 6. #define GPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */ 7. #define GPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */ 8. #define GPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */ 9. #define GPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */ 10. #define GPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */ 11. #define GPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */ 12. #define GPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */ 13. #define GPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */ 14. #define GPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */ 15. #define GPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */ 16. #define GPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */ 17. #define GPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */ 18. #define GPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */ 19. #define GPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */ 20. #define GPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */ 21. #define GPIO_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */
(2)GPIOSpeed_TypeDef GPIO_Speed;GPIO的速度配置,此項(xiàng)的取值參見本頭文件GPIOSpeed_TypeDef枚舉的定義,其中對(duì)應(yīng)3個(gè)速度:10MHz、2MHz、50MHz; 1. /** 2. * @brief Output Maximum frequency selection 3. */ 4. 5. typedef enum 6. { 7. GPIO_Speed_10MHz = 1, 8. GPIO_Speed_2MHz, 9. GPIO_Speed_50MHz 10. }GPIOSpeed_TypeDef;
(3)GPIOMode_TypeDef GPIO_Mode;為GPIO的工作模式配置,其取值參見本頭文件GPIOMode_TypeDef枚舉的定義,STM32 的GPIO共有8種工作模式,分別是GPIO_Mode_AIN(模擬輸入)、GPIO_Mode_IN_FLOATING(輸入浮空)、GPIO_Mode_IPD(輸入下拉)、GPIO_Mode_IPU(輸入上拉)、GPIO_Mode_Out_OD(開漏輸出)、GPIO_Mode_Out_PP(推挽輸出)、GPIO_Mode_AF_OD(開漏復(fù)用功能)、GPIO_Mode_AF_PP(推挽復(fù)用功能)。 1. /** 2. * @brief Configuration Mode enumeration 3. */ 4. 5. typedef enum 6. { GPIO_Mode_AIN = 0x0, 7. GPIO_Mode_IN_FLOATING = 0x04, 8. GPIO_Mode_IPD = 0x28, 9. GPIO_Mode_IPU = 0x48, 10. GPIO_Mode_Out_OD = 0x14, 11. GPIO_Mode_Out_PP = 0x10, 12. GPIO_Mode_AF_OD = 0x1C, 13. GPIO_Mode_AF_PP = 0x18 14. }GPIOMode_TypeDef;
2、開啟APB2外設(shè)時(shí)鐘使能,(ARM與C51單片機(jī)不同的是,不用外設(shè)的時(shí)候,如IO口、ADC、定時(shí)器等等,都是禁止時(shí)鐘的,以達(dá)到節(jié)能的目的,只有要用到的外設(shè),才開啟它的時(shí)鐘。)即調(diào)用void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph,FunctionalState NewState);函數(shù),此函數(shù)是在stm32f10x_rcc.c文件中定義的。其中第一個(gè)參數(shù)指要打開哪一組GPIO的時(shí)鐘,取值參見stm32f10x_rcc.h文件中的宏定義,第二個(gè)參數(shù)為打開或關(guān)閉使能,取值參見stm32f10x.h文件中的定義,其中ENABLE代表開啟使能,DISABLE代表關(guān)閉使能。 stm32f10x_rcc.c: 1. /** 2. * @brief Enables or disables the High Speed APB (APB2) peripheral clock. 3. * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock. 4. * This parameter can be any combination of the following values: 5. * @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB, 6. * RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE, 7. * RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1, 8. * RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1, 9. * RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3, 10. * RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17, 11. * RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11 12. * @param NewState: new state of the specified peripheral clock. 13. * This parameter can be: ENABLE or DISABLE. 14. * @retval None 15. */ 16. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState) 17. { 18. /* Check the parameters */ 19. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph)); 20. assert_param(IS_FUNCTIONAL_STATE(NewState)); 21. if (NewState != DISABLE) 22. { 23. RCC->APB2ENR |= RCC_APB2Periph; 24. } 25. else 26. { 27. RCC->APB2ENR &= ~RCC_APB2Periph; 28. } 29. }
stm32f10x_rcc.h 1. /** @defgroup APB2_peripheral 2. * @{ 3. */ 4. 5. #define RCC_APB2Periph_AFIO ((uint32_t)0x00000001) 6. #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004) 7. #define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008) 8. #define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010) 9. #define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020) 10. #define RCC_APB2Periph_GPIOE ((uint32_t)0x00000040) 11. #define RCC_APB2Periph_GPIOF ((uint32_t)0x00000080) 12. #define RCC_APB2Periph_GPIOG ((uint32_t)0x00000100) 13. #define RCC_APB2Periph_ADC1 ((uint32_t)0x00000200) 14. #define RCC_APB2Periph_ADC2 ((uint32_t)0x00000400) 15. #define RCC_APB2Periph_TIM1 ((uint32_t)0x00000800) 16. #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000) 17. #define RCC_APB2Periph_TIM8 ((uint32_t)0x00002000) 18. #define RCC_APB2Periph_USART1 ((uint32_t)0x00004000) 19. #define RCC_APB2Periph_ADC3 ((uint32_t)0x00008000) 20. #define RCC_APB2Periph_TIM15 ((uint32_t)0x00010000) 21. #define RCC_APB2Periph_TIM16 ((uint32_t)0x00020000) 22. #define RCC_APB2Periph_TIM17 ((uint32_t)0x00040000) 23. #define RCC_APB2Periph_TIM9 ((uint32_t)0x00080000) 24. #define RCC_APB2Periph_TIM10 ((uint32_t)0x00100000) 25. #define RCC_APB2Periph_TIM11 ((uint32_t)0x00200000)
stm32f10x.h: [cpp] view plain copy 1. typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
3、設(shè)置GPIO_InitTypeDef結(jié)構(gòu)體三個(gè)成員的值,取值見第1條;
4、調(diào)用void GPIO_Init(GPIO_TypeDef* GPIOx,GPIO_InitTypeDef* GPIO_InitStruct);函數(shù)配置GPIO,此函數(shù)是在stm32f10x_gpio.c文件中定義的,其中第一個(gè)參數(shù)代表要配置哪組GPIO,取值參見stm32f10x.h文件中的定義,第二個(gè)參數(shù)是第1步定義的GPIO的初始化類型結(jié)構(gòu)體。 stm32f10x_gpio.c: 1. /** 2. * @brief Initializes the GPIOx peripheral according to the specified 3. * parameters in the GPIO_InitStruct. 4. * @param GPIOx: where x can be (A..G) to select the GPIO peripheral. 5. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that 6. * contains the configuration information for the specified GPIO peripheral. 7. * @retval None 8. */ 9. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) 10. //函數(shù)體省略
stm32f10x.h: 1. //以上內(nèi)容省略 2. #define GPIOA ((GPIO_TypeDef *) GPIOA_BASE) 3. #define GPIOB ((GPIO_TypeDef *) GPIOB_BASE) 4. #define GPIOC ((GPIO_TypeDef *) GPIOC_BASE) 5. #define GPIOD ((GPIO_TypeDef *) GPIOD_BASE) 6. #define GPIOE ((GPIO_TypeDef *) GPIOE_BASE) 7. #define GPIOF ((GPIO_TypeDef *) GPIOF_BASE) 8. #define GPIOG ((GPIO_TypeDef *) GPIOG_BASE) 9. //以下內(nèi)容省略
1. /** 2. * @brief General Purpose I/O 3. */ 4. 5. typedef struct 6. { 7. __IO uint32_t CRL; 8. __IO uint32_t CRH; 9. __IO uint32_t IDR; 10. __IO uint32_t ODR; 11. __IO uint32_t BSRR; 12. __IO uint32_t BRR; 13. __IO uint32_t LCKR; 14. } GPIO_TypeDef;
以上4步即完成了對(duì)STM32 GPIO的配置,以下為這4部的示例代碼: 1. GPIO_InitTypeDef GPIO_InitStructure; 2. 3. /* GPIOD Periph clock enable */ 4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); 5. 6. /* Configure PD0 and PD2 in output pushpull mode */ 7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2; 8. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 9. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 10. GPIO_Init(GPIOD, &GPIO_InitStructure);
完成GPIO的配置后,就可以使用了。初學(xué)者操作GPIO最常見的就是讓GPIO輸出高、低電平來控制LED的亮、滅。 GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);函數(shù)就是置位GPIO,即讓相應(yīng)的GPIO輸出高電平;對(duì)應(yīng)的,void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)函數(shù)是讓GPIO復(fù)位的。兩個(gè)參數(shù)在前面講過,就不用多說了吧。 stm32f10x_gpio.c: 1. /** 2. * @brief Sets the selected data port bits. 3. * @param GPIOx: where x can be (A..G) to select the GPIO peripheral. 4. * @param GPIO_Pin: specifies the port bits to be written. 5. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15). 6. * @retval None 7. */ 8. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) 9. { 10. /* Check the parameters */ 11. assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); 12. assert_param(IS_GPIO_PIN(GPIO_Pin)); 13. 14. GPIOx->BSRR = GPIO_Pin; 15. } 16. 17. /** 18. * @brief Clears the selected data port bits. 19. * @param GPIOx: where x can be (A..G) to select the GPIO peripheral. 20. * @param GPIO_Pin: specifies the port bits to be written. 21. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15). 22. * @retval None 23. */ 24. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) 25. { 26. /* Check the parameters */ 27. assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); 28. assert_param(IS_GPIO_PIN(GPIO_Pin)); 29. 30. GPIOx->BRR = GPIO_Pin; 31. }
|