STM32F411实现两个 LED 交替点亮功能
时间: 2024-06-11 22:10:25 浏览: 5
以下是使用STM32F411实现两个LED交替点亮的代码示例:
```c
#include "stm32f4xx.h"
void Delay(__IO uint32_t nCount);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIOA和GPIOC时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
// 配置PA0和PC1为输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while (1)
{
// 点亮PA0
GPIO_SetBits(GPIOA, GPIO_Pin_0);
// 延时
Delay(0xFFFFF);
// 熄灭PA0,点亮PC1
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
GPIO_SetBits(GPIOC, GPIO_Pin_1);
// 延时
Delay(0xFFFFF);
// 熄灭PC1
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
}
}
// 延时函数
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
```
上述代码中,使用了GPIOA的0号引脚和GPIOC的1号引脚来实现两个LED的交替点亮。在主函数中,首先配置了GPIOA的0号引脚和GPIOC的1号引脚为输出模式,并且使能了GPIOA和GPIOC的时钟。然后在无限循环中,先点亮GPIOA的0号引脚,再延时一段时间,然后熄灭GPIOA的0号引脚,点亮GPIOC的1号引脚,再延时一段时间,最后熄灭GPIOC的1号引脚,完成了两个LED的交替点亮。其中,延时函数Delay使用了一个简单的计数循环实现,可以根据需要修改延时时间。