標題: 共享一個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)
下載附件
2018-1-28 20:00 上傳
封裝效果圖
-
-
立創標準封裝.rar
2018-1-28 19:58 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
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.zg4o1577.cn/bbs/) |
Powered by Discuz! X3.1 |
主站蜘蛛池模板:
久久久久亚洲精品
|
一级黄色毛片免费
|
av高清
|
色就干
|
久久精品综合
|
久久久久久国产精品
|
成人性视频免费网站
|
在线91
|
色爱综合网
|
五月精品视频
|
亚洲精品在线视频
|
天天影视网天天综合色在线播放
|
日本超碰
|
日日操av
|
美女久久久久久久
|
久久这里只有精品首页
|
91九色婷婷
|
免费能直接在线观看黄的视频
|
欧美精品一区二区三区四区 在线
|
一片毛片|
99精品视频在线观看
|
真人女人一级毛片免费播放
|
中文字幕日韩欧美一区二区三区
|
九色www|
国产精品18久久久久久白浆动漫
|
国产区精品在线观看
|
精品久久视频
|
日本久久一区二区三区
|
欧美一区二区三区久久精品
|
成人av一区二区三区
|
一级做a爰片性色毛片16
|
久久精品国产一区二区电影
|
超碰在线播
|
免费在线观看黄视频
|
激情国产
|
97视频在线看
|
日韩一区二区三区在线
|
91精品国产一区二区在线观看
|
国产成人99久久亚洲综合精品
|
国产在线a
|
欧美1区2区
|