stm32流水灯程序keil5
时间: 2024-06-11 08:02:12 浏览: 29
STM32流水灯是一个比较简单的程序,可以让你通过控制STM32板上的LED灯实现流水灯效果。下面是基于Keil5编写的STM32流水灯程序:
1. 首先,在Keil5中创建一个新的STM32项目,选择你所使用的芯片型号,例如STM32F103C8T6。
2. 在新建的项目中,添加一个.c文件,例如main.c,并在其中添加如下代码:
```
#include "stm32f10x.h"
#include "delay.h"
#define LED_PORT GPIOB
#define LED_PIN GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
while(1)
{
GPIO_ResetBits(LED_PORT,LED_PIN);
GPIO_SetBits(LED_PORT,GPIO_Pin_12);
delay_ms(300);
GPIO_ResetBits(LED_PORT,LED_PIN);
GPIO_SetBits(LED_PORT,GPIO_Pin_13);
delay_ms(300);
GPIO_ResetBits(LED_PORT,LED_PIN);
GPIO_SetBits(LED_PORT,GPIO_Pin_14);
delay_ms(300);
GPIO_ResetBits(LED_PORT,LED_PIN);
GPIO_SetBits(LED_PORT,GPIO_Pin_15);
delay_ms(300);
}
}
```
3. 在添加的.c文件中,还需要添加一个头文件delay.h,里面包含了延时函数的定义。你可以在网上找到这个头文件并将其加入到你的项目中。
4. 编译并下载该程序到STM32开发板上,然后就可以看到流水灯效果了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)