标题: 共享一个STM32的GPIO设置风格和一套3d封装,大神们拿走不谢 [打印本页]
作者: zhuangfeng 时间: 2018-1-28 20:04
标题: 共享一个STM32的GPIO设置风格和一套3d封装,大神们拿走不谢
这个是百度搜集的一个STM32的GPIO口设置风格,刚入门的萌新们拿走不谢1、首先定义GPIO的初始化类型结构体:GPIO_InitTypeDefGPIO_InitStructure;此结构体的定义是在stm32f10x_gpio.h文件中,其中包括3个成员。
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的哪个或哪些引脚,取值参见本头文件的宏定义,可以同时指定一个或多个要配置的引脚;
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的速度配置,此项的取值参见本头文件GPIOSpeed_TypeDef枚举的定义,其中对应3个速度: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(开漏复用功能)、GPIO_Mode_AF_PP(推挽复用功能)。
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外设时钟使能,(ARM与C51单片机不同的是,不用外设的时候,如IO口、ADC、定时器等等,都是禁止时钟的,以达到节能的目的,只有要用到的外设,才开启它的时钟。)即调用void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph,FunctionalState NewState);函数,此函数是在stm32f10x_rcc.c文件中定义的。其中第一个参数指要打开哪一组GPIO的时钟,取值参见stm32f10x_rcc.h文件中的宏定义,第二个参数为打开或关闭使能,取值参见stm32f10x.h文件中的定义,其中ENABLE代表开启使能,DISABLE代表关闭使能。
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、设置GPIO_InitTypeDef结构体三个成员的值,取值见第1条;
4、调用void GPIO_Init(GPIO_TypeDef* GPIOx,GPIO_InitTypeDef* GPIO_InitStruct);函数配置GPIO,此函数是在stm32f10x_gpio.c文件中定义的,其中第一个参数代表要配置哪组GPIO,取值参见stm32f10x.h文件中的定义,第二个参数是第1步定义的GPIO的初始化类型结构体。
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. //函数体省略
stm32f10x.h:
1. //以上内容省略
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. //以下内容省略
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步即完成了对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的配置后,就可以使用了。初学者操作GPIO最常见的就是让GPIO输出高、低电平来控制LED的亮、灭。
GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);函数就是置位GPIO,即让相应的GPIO输出高电平;对应的,void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)函数是让GPIO复位的。两个参数在前面讲过,就不用多说了吧。
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. }
-
1517140778(1).jpg
(80.99 KB, 下载次数: 89)
封装效果图
-
-
立创标准封装.rar
3.49 MB, 下载次数: 27, 下载积分: 黑币 -5
作者: ccppww806 时间: 2019-1-15 16:11
支持一下
作者: ccppww806 时间: 2019-1-15 16:12
下载下来看看,表示支持
作者: pm1981 时间: 2019-1-16 09:17
支持一下
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |