stm32f103利用循环写一份流水灯代码
时间: 2023-09-06 16:11:52 浏览: 80
以下是使用STM32F103芯片编写的流水灯循环代码:
```
#include "stm32f10x.h"
#define LED_PIN_0 GPIO_Pin_0
#define LED_PIN_1 GPIO_Pin_1
#define LED_PIN_2 GPIO_Pin_2
#define LED_PIN_3 GPIO_Pin_3
#define LED_PORT GPIOC
void delay_ms(uint32_t time);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//启用GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
//将LED引脚配置为推挽输出模式
GPIO_InitStruct.GPIO_Pin = LED_PIN_0 | LED_PIN_1 | LED_PIN_2 | LED_PIN_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStruct);
while (1)
{
//点亮第一个LED
GPIO_SetBits(LED_PORT, LED_PIN_0);
delay_ms(500);
//点亮第二个LED
GPIO_SetBits(LED_PORT, LED_PIN_1);
delay_ms(500);
//点亮第三个LED
GPIO_SetBits(LED_PORT, LED_PIN_2);
delay_ms(500);
//点亮第四个LED
GPIO_SetBits(LED_PORT, LED_PIN_3);
delay_ms(500);
//关闭所有LED
GPIO_ResetBits(LED_PORT, LED_PIN_0 | LED_PIN_1 | LED_PIN_2 | LED_PIN_3);
delay_ms(500);
}
}
void delay_ms(uint32_t time)
{
uint32_t i;
for (i = 0; i < time * 1000; i++)
{
;
}
}
```
本代码使用GPIOC的四个引脚作为LED灯引脚,使用循环控制LED灯的流水效果,延时函数使用简单的for循环实现。当然,使用硬件定时器等其他方式实现延时效果也是可以的。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)