嵌入式开发,Stm32芯片外接RGB模块,实现按下按键,亮起一个灯,按下三次之后,所有灯熄灭
时间: 2024-06-13 10:08:26 浏览: 14
为了实现按下按键亮起一个灯,按下三次之后所有灯熄灭的功能,可以使用状态机的思想,通过判断按键的状态和次数来控制灯的亮灭。具体实现步骤如下:
1.首先需要初始化按键和LED模块,可以使用外部中断和定时器来实现按键的检测和LED的控制。可以参考以下代码:
```c
#include "stm32f10x.h"
#include "led.h"
#define KEY_GPIO_PORT GPIOA
#define KEY_GPIO_PIN GPIO_Pin_0
#define LED_GPIO_PORT GPIOB
#define LED_GPIO_PIN_R GPIO_Pin_0
#define LED_GPIO_PIN_G GPIO_Pin_1
#define LED_GPIO_PIN_B GPIO_Pin_2
uint8_t key_state = 0; // 按键状态
uint8_t key_count = 0; // 按键计数
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN_R | LED_GPIO_PIN_G | LED_GPIO_PIN_B;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
if (GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == RESET)
{
key_state = 1;
}
}
}
```
2.然后需要实现状态机的逻辑,可以参考以下代码:
```c
void LED_Control(uint8_t r, uint8_t g, uint8_t b)
{
if (r)
{
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN_R);
}
else
{
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN_R);
}
if (g)
{
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN_G);
}
else
{
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN_G);
}
if (b)
{
GPIO_SetBits(LED_GPIO_PORT, LED_GPIO_PIN_B);
}
else
{
GPIO_ResetBits(LED_GPIO_PORT, LED_GPIO_PIN_B);
}
}
void StateMachine(void)
{
static uint8_t state = 0;
static uint32_t time = 0;
switch (state)
{
case 0:
if (key_state == 1)
{
key_state = 0;
state = 1;
time = 0;
LED_Control(1, 0, 0);
}
break;
case 1:
if (key_state == 0)
{
state = 0;
LED_Control(0, 0, 0);
}
else
{
time++;
if (time >= 1000)
{
state = 2;
time = 0;
LED_Control(0, 1, 0);
}
}
break;
case 2:
if (key_state == 0)
{
state = 0;
LED_Control(0, 0, 0);
}
else
{
time++;
if (time >= 1000)
{
state = 3;
time = 0;
LED_Control(0, 0, 1);
}
}
break;
case 3:
if (key_state == 0)
{
state = 0;
key_count = 0;
LED_Control(0, 0, 0);
}
else
{
time++;
if (time >= 1000)
{
time = 0;
key_count++;
if (key_count >= 3)
{
state = 4;
LED_Control(0, 0, 0);
}
else
{
state = 1;
LED_Control(1, 0, 0);
}
}
}
break;
case 4:
if (key_state == 0)
{
state = 0;
key_count = 0;
LED_Control(0, 0, 0);
}
break;
default:
break;
}
}
```
3.最后在主函数中循环调用状态机即可:
```c
int main(void)
{
KEY_Init();
LED_Init();
while (1)
{
StateMachine();
}
}
```
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)