找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1773|回复: 2
收起左侧

STM32利用STM32CUBEMX调用HAL库实现LED翻转

[复制链接]
ID:743542 发表于 2023-3-20 01:51 | 显示全部楼层 |阅读模式
学习单片机的第一项都是点灯,而STM公司提供了一套软件可以直接生成代码模板,利用代码模板添加自己的代码调库即可完成项目。故学习之。只做学习分享用途。
作用:利用HAL函数实现LED状态翻转

单片机源程序如下:
  1. #include "main.h"
  2. #include "gpio.h"


  3. void SystemClock_Config(void);
  4. /* USER CODE BEGIN PFP */

  5. /* USER CODE END PFP */

  6. /* Private user code ---------------------------------------------------------*/
  7. /* USER CODE BEGIN 0 */

  8. /* USER CODE END 0 */

  9. /**
  10.   * @brief  The application entry point.
  11.   * @retval int
  12.   */
  13. int main(void)
  14. {
  15.   /* USER CODE BEGIN 1 */

  16.   /* USER CODE END 1 */

  17.   /* MCU Configuration--------------------------------------------------------*/

  18.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  19.   HAL_Init();

  20.   /* USER CODE BEGIN Init */

  21.   /* USER CODE END Init */

  22.   /* Configure the system clock */
  23.   SystemClock_Config();

  24.   /* USER CODE BEGIN SysInit */

  25.   /* USER CODE END SysInit */

  26.   /* Initialize all configured peripherals */
  27.   MX_GPIO_Init();
  28.   /* USER CODE BEGIN 2 */

  29.   /* USER CODE END 2 */

  30.   /* Infinite loop */
  31.   /* USER CODE BEGIN WHILE */
  32.   while (1)
  33.   {
  34.     /* USER CODE END WHILE */
  35.     /* USER CODE BEGIN 3 */
  36.                 HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
  37.                 HAL_Delay(1000);
  38.                 HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
  39.                 HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  40.                 HAL_Delay(1000);
  41.                 HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  42.                 HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
  43.                 HAL_Delay(1000);
  44.                 HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
  45.   }
  46.   /* USER CODE END 3 */
  47. }

  48. /**
  49.   * @brief System Clock Configuration
  50.   * @retval None
  51.   */
  52. void SystemClock_Config(void)
  53. {
  54.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  55.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  56.   /** Initializes the RCC Oscillators according to the specified parameters
  57.   * in the RCC_OscInitTypeDef structure.
  58.   */
  59.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  60.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  61.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  62.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  63.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  64.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  65.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  66.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  67.   {
  68.     Error_Handler();
  69.   }

  70.   /** Initializes the CPU, AHB and APB buses clocks
  71.   */
  72.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  73.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  74.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  75.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  76.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  77.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  78.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  79.   {
  80.     Error_Handler();
  81.   }
  82. }

  83. /* USER CODE BEGIN 4 */

  84. /* USER CODE END 4 */

  85. /**
  86.   * @brief  This function is executed in case of error occurrence.
  87.   * @retval None
  88.   */
  89. void Error_Handler(void)
  90. {
  91.   /* USER CODE BEGIN Error_Handler_Debug */
  92.   /* User can add his own implementation to report the HAL error return state */
  93.   __disable_irq();
  94.   while (1)
  95.   {
  96.   }
  97.   /* USER CODE END Error_Handler_Debug */
  98. }

  99. #ifdef  USE_FULL_ASSERT
  100. /**
  101.   * @brief  Reports the name of the source file and the source line number
  102.   *         where the assert_param error has occurred.
  103.   * @param  file: pointer to the source file name
  104.   * @param  line: assert_param error line source number
  105.   * @retval None
  106.   */
  107. void assert_failed(uint8_t *file, uint32_t line)
  108. {
  109.   /* USER CODE BEGIN 6 */
  110.   /* User can add his own implementation to report the file name and line number,
  111.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  112.   /* USER CODE END 6 */
  113. }
  114. #endif /* USE_FULL_ASSERT */
复制代码

Keil代码下载:
代码.7z (267.48 KB, 下载次数: 11)


回复

使用道具 举报

ID:291668 发表于 2023-3-20 11:23 | 显示全部楼层
用的哪家的开发板?
回复

使用道具 举报

ID:743542 发表于 2023-6-9 17:45 | 显示全部楼层
li64331218 发表于 2023-3-20 11:23
用的哪家的开发板?

都行,最小系统也可以
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表