C语言 尝试闪烁LED STM32Cube IDE STM32F411E-DISCO



我试图通过在板上闪烁LED (PD15)来开始使用STM32Cube IDE。我通过选择正确的板生成了启动C代码,然后在主循环中添加了以下代码:

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

我知道板工作,因为我可以运行示例代码"演示"它使用了所有的led,不幸的是没有提供光源。我认为问题可能是与自动生成的代码,所以这里是完整的代码:


#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 %drn", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

从原始帖子上的线程,我的问题是我没有正确初始化GPIO端口,以下行从HAL_Init()行下方缺失:

__HAL_RCC_GPIOD_CLK_ENABLE();

通过更改,我可以控制led

最新更新