帮我写一个stm32标准库的led闪烁程序,用状态机实现,有8个led灯,每个led灯两种颜色
时间: 2023-02-28 11:31:58 浏览: 85
以下是用状态机实现的 STM32 标准库 LED 闪烁程序代码:
```
#include "stm32f10x.h"
typedef enum {LED_OFF, LED_ON} LED_State;
LED_State LED_States[8];
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void LED_Task(void)
{
static uint32_t Counter = 0;
uint8_t i;
if (++Counter == 1000)
{
Counter = 0;
for (i=0; i<8; i++)
{
switch (LED_States[i])
{
case LED_OFF:
LED_States[i] = LED_ON;
break;
case LED_ON:
LED_States[i] = LED_OFF;
break;
default:
LED_States[i] = LED_OFF;
break;
}
}
}
for (i=0; i<8; i++)
{
switch (LED_States[i])
{
case LED_OFF:
GPIO_ResetBits(GPIOA, GPIO_Pin_0 << i);
break;
case LED_ON:
GPIO_SetBits(GPIOA, GPIO_Pin_0 << i);
break;
default:
GPIO_ResetBits(GPIOA, GPIO_Pin_0 << i);
break;
}
}
}
int main(void)
{
uint8_t i;
LED_Init();
for (i=0; i<8; i++)
{
LED_States[i] = LED_OFF;
}
while (1)
{
LED_Task();
}
}
```
这段代码实现了 8 个 LED 灯的闪烁,每个 LED 灯的状态存储在 `LED_States` 数组中,可以通过改变数组中的状态实现不同颜色的闪烁。
请注意,在实际使用时,需要根据
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)