stm32 HAL庫函數——GPIO


void  HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init);
void  HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin);
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState);
void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin);
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);

以上內容復制自stm32f7xx_hal_gpio.h

 

初始化HAL庫

HAL_Init();

 

 

初始化時鍾(正點原子系統文件夾里得函數)

 

void Stm32_Clock_Init(u32 plln,u32 pllm,u32 pllp,u32 pllq)

  //plln:主PLL倍頻系數(PLL倍頻),取值范圍:64~432.

  //pllm:主PLL和音頻PLL分頻系數(PLL之前的分頻),取值范圍:2~63.

  //pllp:系統時鍾的主PLL分頻系數(PLL之后的分頻),取值范圍:2,4,6,8.(僅限這4個值!)

  //pllq:USB/SDIO/隨機數產生器等的主PLL分頻系數(PLL之后的分頻),取值范圍:2~15.

 

使能io口時鍾

__HAL_RCC_GPIOB_CLK_ENABLE();

 

 

 

初始化GPIO模式

void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)

第一個參數填GPIOA GPIOB GPIOC 等等

第二個參數是結構體

typedef struct
{
  uint32_t Pin;       /*!< Specifies the GPIO pins to be configured.
                           This parameter can be any value of @ref GPIO_pins_define */

  uint32_t Mode;      /*!< Specifies the operating mode for the selected pins.
                           This parameter can be a value of @ref GPIO_mode_define */

  uint32_t Pull;      /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.
                           This parameter can be a value of @ref GPIO_pull_define */

  uint32_t Speed;     /*!< Specifies the speed for the selected pins.
                           This parameter can be a value of @ref GPIO_speed_define */

  uint32_t Alternate;  /*!< Peripheral to be connected to the selected pins. 
                            This parameter can be a value of @ref GPIO_Alternate_function_selection */
}GPIO_InitTypeDef;

比如結構體中Speed變量有如下幾種宏定義:

/** @defgroup GPIO_speed_define  GPIO speed define
  * @brief GPIO Output Maximum frequency
  * @{
  */  
#define  GPIO_SPEED_FREQ_LOW         ((uint32_t)0x00000000U)  /*!< Low speed     */
#define  GPIO_SPEED_FREQ_MEDIUM      ((uint32_t)0x00000001U)  /*!< Medium speed  */
#define  GPIO_SPEED_FREQ_HIGH        ((uint32_t)0x00000002U)  /*!< Fast speed    */
#define  GPIO_SPEED_FREQ_VERY_HIGH   ((uint32_t)0x00000003U)  /*!< High speed    */

至於結構體里面其他變量的宏定義 hal_gpio.h中又詳細的說明,本文不一一列舉

 

下面是一個例子:

GPIO_InitTypeDef    GPIO_Initure; 
GPIO_Initure.Pin=GPIO_PIN_0;            //PB0 
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;    //推挽輸出 
GPIO_Initure.Pull=GPIO_PULLUP;                            //上拉 
GPIO_Initure.Speed=GPIO_SPEED_HIGH;                //高速 
HAL_GPIO_Init(GPIOB,&GPIO_Initure); 

 

 

ODR 寄存器

void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 

 

 

BSRR寄存器

void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin,GPIO_PinState PinState); 

后面的參數PinState只有兩個成員:GPIO_PIN_SET GPIO_PIN_RESET

 

 

ODR 和 BSRR 寄存器的區別:

對於 BSRR 寄存器,你寫 0 的話,對 IO 口電平是沒有任何影響的。
我們要設置某個 IO 口電平,只需要相關位設置為 1 即可。而 ODR 寄存器,我們要設置某個 IO口電平,我們首先需要讀出來 ODR 寄存器的值,
然后對整個 ODR 寄存器重新賦值來達到設置某個或者某些 IO 口的目的,而 BSRR 寄存器,我們就不需要先讀,而是直接設置即可,這在多任務實時操作系統中作用很大。

 

 

讀取IO口電平

GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 

 

 

GPIO 相關的函數我們先講解到這里。雖然 IO 操作步驟很簡單,這里我們還是做個概括性的總結,操作步驟為: 

1) 使能 IO 口時鍾,調用函數為__HAL_RCC_GPIOX_CLK_ENABLE(其中 X=A~K)。 

2) 初始化 IO 參數。調用函數 HAL_GPIO_Init(); 

3) 操作 IO 輸入輸出。操作 IO 的方法就是上面我們講解的方法。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

void  HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM