找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 422|回复: 4
打印 上一主题 下一主题
收起左侧

STM32单片机无法点亮LED

[复制链接]
跳转到指定楼层
楼主
单片机小白入门配置过程:
使用STM32Cubex进行配置,GPIOB5为output状态。期间时钟树未更改

代码编写:
使用HAL库进行编写,将GPIOB进行SET后延时500再RESET再延时500。
问题:烧录后未出现闪烁

(代码和原理图见附件)

51hei图片_20240206122505.png (323.08 KB, 下载次数: 52)

51hei图片_20240206122505.png

屏幕截图 2024-02-06 122544.png (115.41 KB, 下载次数: 55)

屏幕截图 2024-02-06 122544.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:384109 发表于 2024-2-6 16:02 | 只看该作者
代码写错地方了
回复

使用道具 举报

板凳
ID:866313 发表于 2024-2-7 01:48 | 只看该作者
使用的是正点原子的精英开发板吗?
你应该把代码的154~157这四行剪切,然后粘贴到main函数的while(1)循环中,而不是放在Error_Handler函数中,main.c完整源代码如下(注意100~103这4行代码所在位置):
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   *        硬件平台:正点原子STM32F1精英版
  18.   *        引脚分配:PB5-->红色LED(丝印DS0)(引脚输出低电平点亮)
  19.   *                   PE5-->绿色LED(丝印DS1)(引脚输出低电平点亮)
  20.   ******************************************************************************
  21.   */

  22. /* USER CODE END Header */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "main.h"

  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */

  27. /* USER CODE END Includes */

  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */

  30. /* USER CODE END PTD */

  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */

  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */

  36. /* USER CODE END PM */

  37. /* Private variables ---------------------------------------------------------*/

  38. /* USER CODE BEGIN PV */

  39. /* USER CODE END PV */

  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. /* USER CODE BEGIN PFP */

  44. /* USER CODE END PFP */

  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */

  47. /* USER CODE END 0 */

  48. /**
  49.   * @brief  The application entry point.
  50.   * @retval int
  51.   */
  52. int main(void)
  53. {
  54.   /* USER CODE BEGIN 1 */

  55.   /* USER CODE END 1 */

  56.   /* MCU Configuration--------------------------------------------------------*/

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

  59.   /* USER CODE BEGIN Init */

  60.   /* USER CODE END Init */

  61.   /* Configure the system clock */
  62.   SystemClock_Config();

  63.   /* USER CODE BEGIN SysInit */

  64.   /* USER CODE END SysInit */

  65.   /* Initialize all configured peripherals */
  66.   MX_GPIO_Init();
  67.   /* USER CODE BEGIN 2 */

  68.   /* USER CODE END 2 */

  69.   /* Infinite loop */
  70.   /* USER CODE BEGIN WHILE */
  71.   while (1)
  72.   {
  73.     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);   //点亮红色LED
  74.     HAL_Delay(500);//延时500ms
  75.     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);     //熄灭红色LED
  76.     HAL_Delay(500);//延时500ms
  77.     /* USER CODE END WHILE */

  78.     /* USER CODE BEGIN 3 */
  79.   }
  80.   /* USER CODE END 3 */
  81. }

  82. /**
  83.   * @brief System Clock Configuration
  84.   * @retval None
  85.   */
  86. void SystemClock_Config(void)
  87. {
  88.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  89.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  90.   /** Initializes the RCC Oscillators according to the specified parameters
  91.   * in the RCC_OscInitTypeDef structure.
  92.   */
  93.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  94.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  95.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  96.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  97.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  98.   {
  99.     Error_Handler();
  100.   }
  101.   /** Initializes the CPU, AHB and APB buses clocks
  102.   */
  103.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  104.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  105.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  106.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  107.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  108.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  109.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  110.   {
  111.     Error_Handler();
  112.   }
  113. }

  114. /**
  115.   * @brief GPIO Initialization Function
  116.   * @param None
  117.   * @retval None
  118.   */
  119. static void MX_GPIO_Init(void)
  120. {
  121.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  122.   /* GPIO Ports Clock Enable */
  123.   __HAL_RCC_GPIOE_CLK_ENABLE();
  124.   __HAL_RCC_GPIOB_CLK_ENABLE();

  125.   /*Configure GPIO pin Output Level */
  126.   HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);

  127.   /*Configure GPIO pin Output Level */
  128.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);

  129.   /*Configure GPIO pin : PE5 */
  130.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  131.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  132.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  133.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  134.   HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  135.   /*Configure GPIO pin : PB5 */
  136.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  137.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  138.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  139.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  140.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  141. }

  142. /* USER CODE BEGIN 4 */

  143. /* USER CODE END 4 */

  144. /**
  145.   * @brief  This function is executed in case of error occurrence.
  146.   * @retval None
  147.   */
  148. void Error_Handler(void)
  149. {
  150.   /* USER CODE BEGIN Error_Handler_Debug */
  151.   /* User can add his own implementation to report the HAL error return state */
  152.   __disable_irq();
  153.   while (1)
  154.   {
  155.   }
  156.   /* USER CODE END Error_Handler_Debug */
  157. }

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

  174. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
回复

使用道具 举报

地板
ID:1111239 发表于 2024-2-21 14:00 | 只看该作者
延时时间给的太少了吧, 改成100000试试
回复

使用道具 举报

5#
ID:883242 发表于 2024-2-21 19:01 | 只看该作者
HAL_Delay()是用systick实现的,你看看你的151行写了个啥?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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