[STM32F4] ST的HAL库抽象度非常高

[复制链接]
763|7
 楼主| yiyigirl2014 发表于 2019-10-31 21:42 | 显示全部楼层 |阅读模式
ST, rc, ni, TE, TI
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"

  3. /** @addtogroup STM32F4xx_HAL_Examples
  4.   * @{
  5.   */

  6. /** @addtogroup GPIO_EXTI
  7.   * @{
  8.   */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* Private define ------------------------------------------------------------*/
  11. /* Private macro -------------------------------------------------------------*/
  12. /* Private variables ---------------------------------------------------------*/
  13. /* Private function prototypes -----------------------------------------------*/
  14. static void SystemClock_Config(void);
  15. static void Error_Handler(void);
  16. static void EXTILine0_Config(void);

  17. /* Private functions ---------------------------------------------------------*/

  18. /**
  19.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program
  20.   * @param  None
  21.   * @retval None
  22.   */
  23. int main(void)
  24. {
  25. /* This sample code shows how to use STM32F4xx GPIO HAL API to toggle PD12, PD13,
  26.     PD14, and PD14 IOs (connected to LED4, LED3, LED5 and LED6 on STM32F401C-DISCO board (MB1115B))
  27.     in an infinite loop.
  28.     To proceed, 3 steps are required: */

  29.   /* STM32F4xx HAL library initialization:
  30.        - Configure the Flash prefetch, instruction and Data caches
  31.        - Configure the Systick to generate an interrupt each 1 msec
  32.        - Set NVIC Group Priority to 4
  33.        - Global MSP (MCU Support Package) initialization
  34.      */
  35.   HAL_Init();

  36.   /* Configure LED3, LED4, LED5 and LED6 */
  37.   BSP_LED_Init(LED3);
  38.   BSP_LED_Init(LED4);
  39.   BSP_LED_Init(LED5);
  40.   BSP_LED_Init(LED6);
  41.   
  42.   /* Configure the system clock to 84 MHz */
  43.   SystemClock_Config();
  44.    
  45.   /* Configure EXTI Line0 (connected to PA0 pin) in interrupt mode */
  46.   EXTILine0_Config();
  47.   
  48.   /* Infinite loop */
  49.   while (1)
  50.   {
  51.   }
  52. }

  53. /**
  54.   * @brief  System Clock Configuration
  55.   *         The system Clock is configured as follow :
  56.   *            System Clock source            = PLL (HSE)
  57.   *            SYSCLK(Hz)                     = 84000000
  58.   *            HCLK(Hz)                       = 84000000
  59.   *            AHB Prescaler                  = 1
  60.   *            APB1 Prescaler                 = 2
  61.   *            APB2 Prescaler                 = 1
  62.   *            HSE Frequency(Hz)              = 8000000
  63.   *            PLL_M                          = 8
  64.   *            PLL_N                          = 336
  65.   *            PLL_P                          = 4
  66.   *            PLL_Q                          = 7
  67.   *            VDD(V)                         = 3.3
  68.   *            Main regulator output voltage  = Scale2 mode
  69.   *            Flash Latency(WS)              = 2
  70.   * @param  None
  71.   * @retval None
  72.   */
  73. static void SystemClock_Config(void)
  74. {
  75.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  76.   RCC_OscInitTypeDef RCC_OscInitStruct;

  77.   /* Enable Power Control clock */
  78.   __HAL_RCC_PWR_CLK_ENABLE();
  79.   
  80.   /* The voltage scaling allows optimizing the power consumption when the device is
  81.      clocked below the maximum system frequency, to update the voltage scaling value
  82.      regarding system frequency refer to product datasheet.  */
  83.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  84.   
  85.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  86.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  87.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  88.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  89.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  90.   RCC_OscInitStruct.PLL.PLLM = 8;
  91.   RCC_OscInitStruct.PLL.PLLN = 336;
  92.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  93.   RCC_OscInitStruct.PLL.PLLQ = 7;
  94.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  95.   {
  96.     Error_Handler();
  97.   }

  98.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  99.      clocks dividers */
  100.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  101.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  102.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  103.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  
  104.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  105.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  106.   {
  107.     Error_Handler();
  108.   }
  109. }

  110. /**
  111.   * @brief  Configures EXTI Line0 (connected to PA0 pin) in interrupt mode
  112.   * @param  None
  113.   * @retval None
  114.   */
  115. static void EXTILine0_Config(void)
  116. {
  117.   GPIO_InitTypeDef   GPIO_InitStructure;

  118.   /* Enable GPIOA clock */
  119.   __HAL_RCC_GPIOA_CLK_ENABLE();
  120.   
  121.   /* Configure PA0 pin as input floating */
  122.   GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
  123.   GPIO_InitStructure.Pull = GPIO_NOPULL;
  124.   GPIO_InitStructure.Pin = GPIO_PIN_0;
  125.   HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

  126.   /* Enable and set EXTI Line0 Interrupt to the lowest priority */
  127.   HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
  128.   HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  129. }

  130. /**
  131.   * @brief EXTI line detection callbacks
  132.   * @param GPIO_Pin: Specifies the pins connected EXTI line
  133.   * @retval None
  134.   */
  135. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  136. {
  137.   if(GPIO_Pin == KEY_BUTTON_PIN)
  138.   {
  139.     /* Toggle LED3 */
  140.     BSP_LED_Toggle(LED3);
  141.     /* Toggle LED4 */
  142.     BSP_LED_Toggle(LED4);   
  143.     /* Toggle LED5 */
  144.     BSP_LED_Toggle(LED5);   
  145.     /* Toggle LED6 */
  146.     BSP_LED_Toggle(LED6);
  147.   }
  148. }

  149. /**
  150.   * @brief  This function is executed in case of error occurrence.
  151.   * @param  None
  152.   * @retval None
  153.   */
  154. static void Error_Handler(void)
  155. {
  156.   /* Turn LED5 on */
  157.   BSP_LED_On(LED5);
  158.   while(1)
  159.   {
  160.   }
  161. }

  162. #ifdef  USE_FULL_ASSERT
  163. /**
  164.   * @brief  Reports the name of the source file and the source line number
  165.   *         where the assert_param error has occurred.
  166.   * @param  file: pointer to the source file name
  167.   * @param  line: assert_param error line source number
  168.   * @retval None
  169.   */
  170. void assert_failed(uint8_t* file, uint32_t line)
  171. {
  172.   /* User can add his own implementation to report the file name and line number,
  173.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  174.   /* Infinite loop */
  175.   while (1)
  176.   {
  177.   }
  178. }
  179. #endif

  180. /**
  181.   * @}
  182.   */

  183. /**
  184.   * @}
  185.   */

  186. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


 楼主| yiyigirl2014 发表于 2019-10-31 21:43 | 显示全部楼层
一般应该有个中断处理函数的,找啊找不到,原来封装起来了,只需要自己实现那个中断回调函数就行了。
 楼主| yiyigirl2014 发表于 2019-10-31 21:43 | 显示全部楼层
这个概念如果不懂,很多人会糊涂,搞懂了,会觉得真容易,变的更简单了
wanduzi 发表于 2019-10-31 21:44 | 显示全部楼层
是啊,我一直很纳闷怎么没有清理中断的地方。原来如此。
heimaojingzhang 发表于 2019-11-19 11:02 | 显示全部楼层
非常感谢楼主分享
guanjiaer 发表于 2019-11-19 11:09 | 显示全部楼层
非常感谢楼主分享
木木guainv 发表于 2019-11-19 11:18 | 显示全部楼层
非常感谢楼主分享
xiaoqizi 发表于 2019-11-19 11:40 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部