STM32CUBEIDE点灯代码
时间: 2023-07-02 13:18:47 浏览: 124
以下是一个简单的STM32CubeIDE点灯示例代码,使用的是STM32F4 Discovery开发板和HAL库:
```c
#include "main.h"
/* 定义LED灯引脚 */
#define LED_PIN GPIO_PIN_12
#define LED_PORT GPIOD
/* 定义延时函数 */
void delay(uint32_t time)
{
while(time--);
}
/* 主函数 */
int main(void)
{
/* 初始化HAL库 */
HAL_Init();
/* 初始化时钟 */
SystemClock_Config();
/* 初始化GPIO */
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
/* 循环点灯 */
while (1)
{
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
delay(1000000);
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
delay(1000000);
}
}
```
代码中先定义了LED灯的引脚,然后定义了一个简单的延时函数。在主函数中,先初始化了HAL库和时钟,然后初始化了GPIO,设置为输出模式。最后在一个无限循环中,不断地点亮和熄灭LED灯,并且通过延时函数控制亮灯和熄灯的时间。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)