使用STM32F103R6芯片4个LED循环点亮。
时间: 2024-05-03 18:22:26 浏览: 13
使用STM32F103R6芯片点亮4个LED可以按照以下步骤进行:
1. 配置GPIO引脚为输出模式。
2. 初始化系统时钟和GPIO引脚。
3. 在一个循环中,依次点亮4个LED,每个LED点亮一段时间后熄灭,再点亮下一个LED。
下面是一个简单的示例代码:
```
#include "stm32f10x.h"
#define LED1_PIN GPIO_Pin_0
#define LED2_PIN GPIO_Pin_1
#define LED3_PIN GPIO_Pin_2
#define LED4_PIN GPIO_Pin_3
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
int main(void)
{
GPIO_Configuration();
while (1)
{
GPIO_SetBits(GPIOA, LED1_PIN);
Delay(1000000);
GPIO_ResetBits(GPIOA, LED1_PIN);
GPIO_SetBits(GPIOA, LED2_PIN);
Delay(1000000);
GPIO_ResetBits(GPIOA, LED2_PIN);
GPIO_SetBits(GPIOA, LED3_PIN);
Delay(1000000);
GPIO_ResetBits(GPIOA, LED3_PIN);
GPIO_SetBits(GPIOA, LED4_PIN);
Delay(1000000);
GPIO_ResetBits(GPIOA, LED4_PIN);
}
}
```
在上面的代码中,我们使用了GPIO_InitStructure结构体来配置GPIO引脚的模式和速度。在主函数中,我们依次点亮4个LED,每个LED点亮1秒后熄灭,再点亮下一个LED。我们可以使用Delay函数来延迟LED点亮的时间,这里的延迟时间是1000000个时钟周期,可以根据需要进行调整。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)