goodwangLib / HAL / STM32 HAL庫

0 0

   

STM32 HAL庫

2020-03-10  goodwangL...
HAL_Init();
  • 1

這是主函數中首要處理的函數:主要用來初始化HAL庫,即用來初始化所有的外圍設備,Flash接口和系統定時器,系統中斷組,初始化低級別硬件這個函數不能修改,return HAL_OK向函數返回一個值,代表函數編譯成功。

HAL_StatusTypeDef HAL_Init(void){  /* Configure Flash prefetch, Instruction cache, Data cache */ #if (INSTRUCTION_CACHE_ENABLE != 0U)   __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();#endif /* INSTRUCTION_CACHE_ENABLE */#if (DATA_CACHE_ENABLE != 0U)   __HAL_FLASH_DATA_CACHE_ENABLE();#endif /* DATA_CACHE_ENABLE */#if (PREFETCH_ENABLE != 0U)  __HAL_FLASH_PREFETCH_BUFFER_ENABLE();#endif /* PREFETCH_ENABLE */  /* Set Interrupt Group Priority */  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);  /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */  HAL_InitTick(TICK_INT_PRIORITY);  /* Init the low level hardware */  HAL_MspInit();  /* Return function status */  return HAL_OK;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

以上是HAL_Init()函數,通過以上的函數源代碼我們可以看到HAL庫的初始化函數是采用條件編譯配置的方式進行初始化的,下面我們來逐個分析其條件編譯:
INSTRUCTION_CACHE_ENABLE配置指令緩存使能;

#define INSTRUCTION_CACHE_ENABLE 1U
  • 1

DATA_CACHE_ENABLE 配置數據緩存使能;

#define  DATA_CACHE_ENABLE            1U
  • 1

PREFETCH_ENABL配置Flash預讀取使能;

#define PREFETCH_ENABLE 1U
  • 1
return HAL_OK
  • 1

這條語句向函數返回一個狀態,在HAL庫中,函數狀態一共有四種狀態,分別是:成功,錯誤,忙碌,超時;具體值如下:

typedef enum { HAL_OK = 0x00U, HAL_ERROR = 0x01U, HAL_BUSY = 0x02U, HAL_TIMEOUT = 0x03U} HAL_StatusTypeDef;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

通過在stm32f4xx_hal_conf.h中可以查看每個條件編譯的預定義值;

#define  VDD_VALUE            ((uint32_t)3300U) /*!< Value of VDD in mv */           #define  TICK_INT_PRIORITY            ((uint32_t)0U)   /*!< tick interrupt priority */            #define  USE_RTOS                     0U     #define  PREFETCH_ENABLE              1U#define  INSTRUCTION_CACHE_ENABLE     1U#define  DATA_CACHE_ENABLE            1U
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

接下是執行的函數是:

SystemClock_Config();//系統時鐘配置
  • 1

其函數源代碼如下:

/** System Clock Configuration*/void SystemClock_Config(void){  RCC_OscInitTypeDef RCC_OscInitStruct;  RCC_ClkInitTypeDef RCC_ClkInitStruct;    /**Configure the main internal regulator output voltage     */  __HAL_RCC_PWR_CLK_ENABLE();  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);    /**Initializes the CPU, AHB and APB busses clocks     */  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;  RCC_OscInitStruct.HSIState = RCC_HSI_ON;  RCC_OscInitStruct.HSICalibrationValue = 16;  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;  RCC_OscInitStruct.PLL.PLLM = 8;  RCC_OscInitStruct.PLL.PLLN = 168;  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;  RCC_OscInitStruct.PLL.PLLQ = 4;  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)  {    Error_Handler();  }    /**Initializes the CPU, AHB and APB busses clocks     */  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)  {    Error_Handler();  }    /**Configure the Systick interrupt time     */  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);    /**Configure the Systick     */  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);  /* SysTick_IRQn interrupt configuration */  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

函數主要調用了和配置兩個結構體RCC_OscInitTypeDef和
RCC_OscInitTypeDef結構體函數:

/** * @brief RCC Internal/External Oscillator (HSE, HSI, LSE and LSI) configuration structure definition */typedef struct{ uint32_t OscillatorType; /*!< The oscillators to be configured. This parameter can be a value of @ref RCC_Oscillator_Type */ uint32_t HSEState; /*!< The new state of the HSE. This parameter can be a value of @ref RCC_HSE_Config */ uint32_t LSEState; /*!< The new state of the LSE. This parameter can be a value of @ref RCC_LSE_Config */ uint32_t HSIState; /*!< The new state of the HSI. This parameter can be a value of @ref RCC_HSI_Config */ uint32_t HSICalibrationValue; /*!< The HSI calibration trimming value (default is RCC_HSICALIBRATION_DEFAULT). This parameter must be a number between Min_Data = 0x00 and Max_Data = 0x1F */ uint32_t LSIState; /*!< The new state of the LSI. This parameter can be a value of @ref RCC_LSI_Config */ RCC_PLLInitTypeDef PLL; /*!< PLL structure parameters */}RCC_OscInitTypeDef;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

以上每個結構體元素都有其所對應的的參數;
OscillatorType 用來配置系統振蕩器,有以下四個值:

#define RCC_OSCILLATORTYPE_NONE            0x00000000U //不使用#define RCC_OSCILLATORTYPE_HSE             0x00000001U //采用外部高速時鐘振蕩器#define RCC_OSCILLATORTYPE_HSI             0x00000002U //采用內部高速時鐘振蕩器#define RCC_OSCILLATORTYPE_LSE             0x00000004U //采用外部低速時鐘振蕩器#define RCC_OSCILLATORTYPE_LSI             0x00000008U //采用內部低速時鐘振蕩器
  • 1
  • 2
  • 3
  • 4
  • 5

HSEState用來配置外部高速始終振蕩器的的狀態,有以下三種狀態:

#define RCC_HSE_OFF 0x00000000U //關閉外部高速時鐘#define RCC_HSE_ON RCC_CR_HSEON //打開外部高速時鐘#define RCC_HSE_BYPASS ((uint32_t)(RCC_CR_HSEBYP | RCC_CR_HSEON))
  • 1
  • 2
  • 3

HSIState/LSEState/LSIState這是結構體元素的參數可以依照以上示例都可以找到:

/** @defgroup RCC_LSE_Config LSE Config  * @{  */#define RCC_LSE_OFF                    0x00000000U#define RCC_LSE_ON                     RCC_BDCR_LSEON#define RCC_LSE_BYPASS                 ((uint32_t)(RCC_BDCR_LSEBYP | RCC_BDCR_LSEON))/**  * @}  *//** @defgroup RCC_HSI_Config HSI Config  * @{  */#define RCC_HSI_OFF                      ((uint8_t)0x00)#define RCC_HSI_ON                       ((uint8_t)0x01)#define RCC_HSICALIBRATION_DEFAULT       0x10U         /* Default HSI calibration trimming value *//**  * @}  *//** @defgroup RCC_LSI_Config LSI Config  * @{  */#define RCC_LSI_OFF                      ((uint8_t)0x00)#define RCC_LSI_ON                       ((uint8_t)0x01)/**  * @}  *//** @defgroup RCC_PLL_Config PLL Config  * @{  */#define RCC_PLL_NONE                      ((uint8_t)0x00)#define RCC_PLL_OFF                       ((uint8_t)0x01)#define RCC_PLL_ON                        ((uint8_t)0x02)/**  * @}  */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

HSICalibrationValue是內部高速始終調整值:這個參數的取值為0~31之間的數;
RCC_PLLInitTypeDef PLL是結構體中調用的另一個結構體,代碼如下:

typedef struct{ uint32_t PLLState; /*!< The new state of the PLL. This parameter can be a value of @ref RCC_PLL_Config */ uint32_t PLLSource; /*!< RCC_PLLSource: PLL entry clock source. This parameter must be a value of @ref RCC_PLL_Clock_Source */ uint32_t PLLM; /*!< PLLM: Division factor for PLL VCO input clock. This parameter must be a number between Min_Data = 0 and Max_Data = 63 */ uint32_t PLLN; /*!< PLLN: Multiplication factor for PLL VCO output clock. This parameter must be a number between Min_Data = 50 and Max_Data = 432 except for STM32F411xE devices where the Min_Data = 192 */ uint32_t PLLP; /*!< PLLP: Division factor for main system clock (SYSCLK). This parameter must be a value of @ref RCC_PLLP_Clock_Divider */ uint32_t PLLQ; /*!< PLLQ: Division factor for OTG FS, SDIO and RNG clocks. This parameter must be a number between Min_Data = 2 and Max_Data = 15 */#if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) || defined(STM32F469xx) ||\ defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) ||\ defined(STM32F413xx) || defined(STM32F423xx) uint32_t PLLR; /*!< PLLR: PLL division factor for I2S, SAI, SYSTEM, SPDIFRX clocks. This parameter is only available in STM32F410xx/STM32F446xx/STM32F469xx/STM32F479xx and STM32F412Zx/STM32F412Vx/STM32F412Rx/STM32F412Cx/STM32F413xx/STM32F423xx devices. This parameter must be a number between Min_Data = 2 and Max_Data = 7 */#endif /* STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */ }RCC_PLLInitTypeDef;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

以上每個結構體元素的參數都定義在文件stm32f4xx_rcc.h文件夾里,具體代碼如下:

/** @defgroup RCC_PLL_Config PLL Config鎖相環時鐘配置的參數  * @{  */#define RCC_PLL_NONE                      ((uint8_t)0x00)#define RCC_PLL_OFF                       ((uint8_t)0x01)#define RCC_PLL_ON                        ((uint8_t)0x02)/**  * @}  *//** @defgroup RCC_PLLP_Clock_Divider PLLP Clock Divider鎖相環分頻  * @{  */#define RCC_PLLP_DIV2                  0x00000002U#define RCC_PLLP_DIV4                  0x00000004U#define RCC_PLLP_DIV6                  0x00000006U#define RCC_PLLP_DIV8                  0x00000008U/**  * @}  *//** @defgroup RCC_PLL_Clock_Source PLL Clock Source鎖相環時鐘的源  * @{  */#define RCC_PLLSOURCE_HSI                RCC_PLLCFGR_PLLSRC_HSI#define RCC_PLLSOURCE_HSE                RCC_PLLCFGR_PLLSRC_HSE/**  * @}  *//** @defgroup RCC_System_Clock_Type System Clock Type  * @{  */#define RCC_CLOCKTYPE_SYSCLK             0x00000001U#define RCC_CLOCKTYPE_HCLK               0x00000002U#define RCC_CLOCKTYPE_PCLK1              0x00000004U#define RCC_CLOCKTYPE_PCLK2              0x00000008U/**  * @}  */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

PLLQ是對OTG FS, SDIO and RNG clocks的分頻因子,參數是2~15之間的數;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); }
  • 1
  • 2
  • 3
  • 4

以上代碼是檢測所調用的結構體函數RCC_OscInitStruct,是否編譯成功;
RCC_ClkInitTypeDef結構體函數:

typedef struct{  uint32_t ClockType;             /*!< The clock to be configured.                                       This parameter can be a value of @ref RCC_System_Clock_Type      */  uint32_t SYSCLKSource;          /*!< The clock source (SYSCLKS) used as system clock.                                       This parameter can be a value of @ref RCC_System_Clock_Source    */  uint32_t AHBCLKDivider;         /*!< The AHB clock (HCLK) divider. This clock is derived from the system clock (SYSCLK).                                       This parameter can be a value of @ref RCC_AHB_Clock_Source       */  uint32_t APB1CLKDivider;        /*!< The APB1 clock (PCLK1) divider. This clock is derived from the AHB clock (HCLK).                                       This parameter can be a value of @ref RCC_APB1_APB2_Clock_Source */  uint32_t APB2CLKDivider;        /*!< The APB2 clock (PCLK2) divider. This clock is derived from the AHB clock (HCLK).                                       This parameter can be a value of @ref RCC_APB1_APB2_Clock_Source */}RCC_ClkInitTypeDef;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

每個結構體所對應的的元素如下:

/** @defgroup RCC_System_Clock_Source System Clock Source * @note The RCC_SYSCLKSOURCE_PLLRCLK parameter is available only for * STM32F446xx devices. * @{ */#define RCC_SYSCLKSOURCE_HSI RCC_CFGR_SW_HSI#define RCC_SYSCLKSOURCE_HSE RCC_CFGR_SW_HSE#define RCC_SYSCLKSOURCE_PLLCLK RCC_CFGR_SW_PLL#define RCC_SYSCLKSOURCE_PLLRCLK ((uint32_t)(RCC_CFGR_SW_0 | RCC_CFGR_SW_1))/** * @} *//** @defgroup RCC_System_Clock_Source_Status System Clock Source Status * @note The RCC_SYSCLKSOURCE_STATUS_PLLRCLK parameter is available only for * STM32F446xx devices. * @{ */#define RCC_SYSCLKSOURCE_STATUS_HSI RCC_CFGR_SWS_HSI /*!< HSI used as system clock */#define RCC_SYSCLKSOURCE_STATUS_HSE RCC_CFGR_SWS_HSE /*!< HSE used as system clock */#define RCC_SYSCLKSOURCE_STATUS_PLLCLK RCC_CFGR_SWS_PLL /*!< PLL used as system clock */#define RCC_SYSCLKSOURCE_STATUS_PLLRCLK ((uint32_t)(RCC_CFGR_SWS_0 | RCC_CFGR_SWS_1)) /*!< PLLR used as system clock *//** * @} *//** @defgroup RCC_AHB_Clock_Source AHB Clock Source * @{ */#define RCC_SYSCLK_DIV1 RCC_CFGR_HPRE_DIV1#define RCC_SYSCLK_DIV2 RCC_CFGR_HPRE_DIV2#define RCC_SYSCLK_DIV4 RCC_CFGR_HPRE_DIV4#define RCC_SYSCLK_DIV8 RCC_CFGR_HPRE_DIV8#define RCC_SYSCLK_DIV16 RCC_CFGR_HPRE_DIV16#define RCC_SYSCLK_DIV64 RCC_CFGR_HPRE_DIV64#define RCC_SYSCLK_DIV128 RCC_CFGR_HPRE_DIV128#define RCC_SYSCLK_DIV256 RCC_CFGR_HPRE_DIV256#define RCC_SYSCLK_DIV512 RCC_CFGR_HPRE_DIV512/** * @} */ /** @defgroup RCC_APB1_APB2_Clock_Source APB1/APB2 Clock Source * @{ */#define RCC_HCLK_DIV1 RCC_CFGR_PPRE1_DIV1#define RCC_HCLK_DIV2 RCC_CFGR_PPRE1_DIV2#define RCC_HCLK_DIV4 RCC_CFGR_PPRE1_DIV4#define RCC_HCLK_DIV8 RCC_CFGR_PPRE1_DIV8#define RCC_HCLK_DIV16 RCC_CFGR_PPRE1_DIV16/** * @} */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  • 1

HAL_SYSTICK_Config用來配置系統定時器中斷時間;

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  • 1

配置系統定時器;

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  • 1

設置中斷優先級;SysTick_IRQn代表終端類型是系統定時器中斷,第一個0代表的是中斷優先級,其取值范圍是0~15,值越小其優先級越高;
第二個0代表子優先級,其取值范圍是0~15,值越小其優先級越高;函數原型如下:

void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
  • 1

接下來是才到初始化GPIO,MX_GPIO_Init();
具體代碼如下:

void MX_GPIO_Init(void){  GPIO_InitTypeDef GPIO_InitStruct;  //首先需要使能GPIO組,這里一共有ABCDE五組GPIO組,  /* GPIO Ports Clock Enable */  __HAL_RCC_GPIOE_CLK_ENABLE();  __HAL_RCC_GPIOC_CLK_ENABLE();  __HAL_RCC_GPIOF_CLK_ENABLE();  __HAL_RCC_GPIOH_CLK_ENABLE();  __HAL_RCC_GPIOA_CLK_ENABLE();  /*Configure GPIO pin Output Level */  HAL_GPIO_WritePin(GPIOF, LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin, GPIO_PIN_RESET);  //調用函數HAL_GPIO_WritePin將F組的LED1_Pin(GPIO_PIN_6)等清零,LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin是宏定義,宏定義的定義在main.文件中,GPIO_PIN_RESET\參數的值是0,代表的是引腳的狀態,清零表示當我們運行程序的時候這些引腳都被設置為低電平;;  /*Configure GPIO pins : PEPin PEPin PEPin */  GPIO_InitStruct.Pin = JOY_D_Pin|JOY_C_Pin|JOY_B_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;  GPIO_InitStruct.Pull = GPIO_PULLUP;  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);  /*Configure GPIO pin : PtPin */  GPIO_InitStruct.Pin = JOY_A_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;  GPIO_InitStruct.Pull = GPIO_PULLUP;  HAL_GPIO_Init(JOY_A_GPIO_Port, &GPIO_InitStruct);  /*Configure GPIO pins : PFPin PFPin PFPin PFPin */  GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin|LED3_Pin|LED4_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;  GPIO_InitStruct.Pull = GPIO_NOPULL;  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);  /*Configure GPIO pin : PtPin */  GPIO_InitStruct.Pin = KEY_USER_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;  GPIO_InitStruct.Pull = GPIO_PULLUP;  HAL_GPIO_Init(KEY_USER_GPIO_Port, &GPIO_InitStruct);  /*Configure GPIO pins : PAPin PAPin */  GPIO_InitStruct.Pin = KEY_WAKEUP_Pin|JOY_CTR_Pin;  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;  GPIO_InitStruct.Pull = GPIO_PULLUP;  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

以上這些結構體是設置引腳的狀態,調用的是以下結構體:

typedef struct{ uint32_t Pin; /*!< Specifies the GPIO pins to be configured.指定所需的引腳,有GPIO_PIN_0~1516個引腳可以配置; This parameter can be any value of @ref GPIO_pins_define */ uint32_t Mode; /*!< Specifies the operating mode for the selected pins.給這個引腳設置模式;模式有輸入,普通上拉、下拉輸出,復用上拉、下拉輸出,模擬模式,外部中斷上升沿、/下降沿觸發模式,外部事件上升沿、/下降沿觸發模式;GPIO_MODE_INPUT GPIO_MODE_OUTPUT_PP GPIO_MODE_OUTPUT_OD GPIO_MODE_AF_PP GPIO_MODE_AF_OD GPIO_MODE_ANALOG GPIO_MODE_IT_RISING GPIO_MODE_IT_FALLING GPIO_MODE_IT_RISING_FALLING GPIO_MODE_EVT_RISING GPIO_MODE_EVT_FALLING GPIO_MODE_EVT_RISING_FALLING uint32_t Pull; /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.指定的引腳是上拉還是下拉GPIO_NOPULL 不處理GPIO_PULLUP 上拉GPIO_PULLDOWN 下拉uint32_t Speed; /*!< Specifies the speed for the selected pins.指定引腳的速度GPIO_SPEED_FREQ_LOW 低 GPIO_SPEED_FREQ_MEDIUM 中GPIO_SPEED_FREQ_HIGH 高GPIO_SPEED_FREQ_VERY_HIGH最高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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

每個引腳配置完之后都執行函數:HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 來初始化所配置的引腳。

  while (1)  {  /* USER CODE END WHILE */  /* USER CODE BEGIN 3 */    /* A - LED1 */    if(HAL_GPIO_ReadPin(JOY_A_GPIO_Port, JOY_A_Pin) == GPIO_PIN_RESET)      HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);    else      HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);    /* B - LED2 */    if(HAL_GPIO_ReadPin(JOY_B_GPIO_Port, JOY_B_Pin) == GPIO_PIN_RESET)      HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);    else      HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);    /* C - LED3 */    if(HAL_GPIO_ReadPin(JOY_C_GPIO_Port, JOY_C_Pin) == GPIO_PIN_RESET)      HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);    else      HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);    /* D - LED4 */    if(HAL_GPIO_ReadPin(JOY_D_GPIO_Port, JOY_D_Pin) == GPIO_PIN_RESET)      HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET);    else      HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);    /* PRESS - LED1 LED2 LED3 LED4 */    if(HAL_GPIO_ReadPin(JOY_CTR_GPIO_Port, JOY_CTR_Pin) == GPIO_PIN_RESET ||       HAL_GPIO_ReadPin(KEY_WAKEUP_GPIO_Port, KEY_WAKEUP_Pin) == GPIO_PIN_RESET ||       HAL_GPIO_ReadPin(KEY_USER_GPIO_Port, KEY_USER_Pin) == GPIO_PIN_RESET)    {      HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);      HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);      HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);      HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET);    }    else    {      HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);      HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);      HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);      HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET);    }  }  /* USER CODE END 3 */}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

以上是對函數的操作,采用if—else結構先對所定義的引腳進行讀操作,然后再進行寫操作,然后再通過讀取按鍵的值來操作LED 的狀態。
以上就是Hal操作GPIO時所涉及到的知識;記錄學習過程,與各位分享。

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。如發現有害或侵權內容,請點擊這里 或 撥打24小時舉報電話:4000070609 與我們聯系。

    猜你喜歡

    0條評論

    發表

    請遵守用戶 評論公約

    類似文章 更多
    喜歡該文的人也喜歡 更多

    fun888 <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <文本链> <文本链> <文本链> <文本链> <文本链> <文本链>