stm32实现三个LED循环闪烁
时间: 2023-09-30 09:05:02 浏览: 216
以下是使用STM32实现三个LED循环闪烁的代码片段:
```
#include "stm32f10x.h"
void delay(uint32_t time);
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while (1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_13);
delay(1000000);
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
GPIO_SetBits(GPIOC, GPIO_Pin_14);
delay(1000000);
GPIO_ResetBits(GPIOC, GPIO_Pin_14);
GPIO_SetBits(GPIOC, GPIO_Pin_15);
delay(1000000);
GPIO_ResetBits(GPIOC, GPIO_Pin_15);
}
}
void delay(uint32_t time)
{
while (time--)
;
}
```
在上面的代码中,我们首先启用了GPIOC的时钟,并初始化三个GPIO引脚(13, 14, 15)作为输出引脚。然后在while循环中,我们依次点亮三个LED,并在每个LED点亮后等待一段时间再将其熄灭,然后从下一个LED开始循环。在这里,我们使用了一个简单的循环延迟函数,它会等待一段时间,直到计数器计数完毕。您可以根据需要调整延迟时间。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)