简体   繁体   English

试图闪烁 LED STM32Cube IDE STM32F411E-DISCO

[英]Trying to blink LED STM32Cube IDE STM32F411E-DISCO

I'm trying to get started with STM32Cube IDE by blinking an LED on the board (PD15).我正在尝试通过使电路板上的 LED (PD15) 闪烁来开始使用 STM32Cube IDE。 I generated the starter C code by selecting the correct board, then added the following to the main loop:我通过选择正确的电路板生成了起始代码 C,然后将以下内容添加到主循环中:

  HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
  HAL_Delay(200);

I know the board works because I can run the example code "Demonstrations" which uses all of the LEDs, unfortunately the source isn't provided.我知道该板可以工作,因为我可以运行使用所有 LED 的示例代码“演示”,不幸的是没有提供源代码。 I think the problem might be with the auto generated code so here is the full code:我认为问题可能出在自动生成的代码上,所以这里是完整的代码:


#include "main.h"
#include "usb_host.h"

void SystemClock_Config(void);

int main(void)
{
  HAL_Init();

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitStruct.Pin = GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
  }
}



/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

From the thread on the original post, my problem was that I was not initializing the GPIO ports properly, and the following line was missing from below the HAL_Init() line:从原始帖子的线程来看,我的问题是我没有正确初始化 GPIO 端口,并且HAL_Init()行下方缺少以下行:

__HAL_RCC_GPIOD_CLK_ENABLE();

With the changes I am able to control the LEDs通过更改,我能够控制 LED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM