久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3445|回復: 3
打印 上一主題 下一主題
收起左側

共享一個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

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:285352 發表于 2019-1-15 16:11 | 只看該作者
支持一下
回復

使用道具 舉報

板凳
ID:285352 發表于 2019-1-15 16:12 | 只看該作者
下載下來看看,表示支持
回復

使用道具 舉報

地板
ID:223999 發表于 2019-1-16 09:17 | 只看該作者
支持一下
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美亚洲国语精品一区二区 | 成人超碰在线 | 综合精品 | 亚洲欧美日韩精品久久亚洲区 | 精品视频网 | 国产三区四区 | 久久成人一区 | 九九精品在线 | 九九热这里只有精品6 | 亚洲 欧美 日韩 精品 | 亚洲精选一区 | 拍拍无遮挡人做人爱视频免费观看 | 二区在线观看 | 国产在线精品免费 | 精品久久久一区 | 在线观看中文字幕亚洲 | 91新视频 | 91传媒在线观看 | 久久久久国产精品午夜一区 | 亚洲影视在线 | 欧美精品在线一区 | 久久九九影视 | aaa精品| 免费在线观看h片 | 欧美日本免费 | 欧美看片| 国产成人网| 久久不射电影网 | 国产精品揄拍一区二区 | 在线国产99 | www国产成人免费观看视频,深夜成人网 | 日本精品一区二区三区视频 | 日韩中文字幕一区 | 精品久久av | 99精品视频一区二区三区 | 精品一区二区三区中文字幕 | 天天色综 | 久久久精品综合 | 欧美激情国产精品 | 欧美中文字幕一区 | 亚洲激情综合 |