stm32 拖尾灯程序
时间: 2023-08-31 15:12:46 浏览: 93
以下是一个简单的STM32拖尾灯程序,可以参考一下:
```c
#include "stm32f10x.h"
#define LED_NUM 8 // 灯的数量
#define DELAY_MS 100 // 灯的延迟时间
int main(void)
{
uint32_t ledState = 0; // 记录灯的状态
uint32_t i = 0;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 使能GPIOC时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // PC0~PC7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 速度50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
while (1)
{
// 点亮灯
for (i = 0; i < LED_NUM; i++)
{
GPIO_SetBits(GPIOC, 1 << i);
ledState |= 1 << i;
delay_ms(DELAY_MS); // 等待一段时间
}
// 熄灭灯
for (i = 0; i < LED_NUM; i++)
{
GPIO_ResetBits(GPIOC, 1 << i);
ledState &= ~(1 << i);
delay_ms(DELAY_MS); // 等待一段时间
}
// 反转灯
for (i = 0; i < LED_NUM; i++)
{
if (ledState & (1 << i))
{
GPIO_ResetBits(GPIOC, 1 << i);
ledState &= ~(1 << i);
}
else
{
GPIO_SetBits(GPIOC, 1 << i);
ledState |= 1 << i;
}
delay_ms(DELAY_MS); // 等待一段时间
}
}
}
void delay_ms(uint32_t delay)
{
uint32_t i = 0;
uint32_t j = 0;
for (i = 0; i < delay; i++)
{
for (j = 0; j < 0x5000; j++)
{
}
}
}
```
这个程序使用了GPIO控制输出,实现了点亮、熄灭和反转灯的效果,每个灯的延迟时间可以通过DELAY_MS宏定义设置。注意:在使用该程序之前,需要先在STM32开发环境中配置好GPIO和系统时钟。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)