四针脚按键控制led灯亮灭stm32
时间: 2023-09-17 18:15:03 浏览: 224
在STM32中,我们可以使用GPIO来控制LED的亮灭。以下是一个示例代码,该代码使用四个按键控制四个LED的亮灭。
首先,我们需要为每个LED和按键分配一个GPIO引脚。在本例中,我们将使用四个LED和四个按键,并将它们分配到以下引脚:
- LED1:PB0
- LED2:PB1
- LED3:PB2
- LED4:PB3
- 按键1:PA0
- 按键2:PA1
- 按键3:PA2
- 按键4:PA3
接下来,我们需要启用GPIO时钟,并将相应的引脚设置为输出或输入模式。在本例中,我们将所有LED设置为输出模式,将所有按键设置为输入模式。
```c
#include "stm32f10x.h"
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// LED1引脚设置为输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// LED2引脚设置为输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// LED3引脚设置为输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// LED4引脚设置为输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 按键1引脚设置为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 按键2引脚设置为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 按键3引脚设置为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 按键4引脚设置为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
```
接下来,我们可以在主函数中编写一个循环,该循环将检查每个按键的状态,并相应地控制每个LED的状态。
```c
int main(void)
{
GPIO_Config();
while(1)
{
// 检查按键1的状态
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
GPIO_SetBits(GPIOB, GPIO_Pin_0); // LED1亮
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_0); // LED1灭
}
// 检查按键2的状态
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == Bit_RESET)
{
GPIO_SetBits(GPIOB, GPIO_Pin_1); // LED2亮
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_1); // LED2灭
}
// 检查按键3的状态
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2) == Bit_RESET)
{
GPIO_SetBits(GPIOB, GPIO_Pin_2); // LED3亮
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_2); // LED3灭
}
// 检查按键4的状态
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == Bit_RESET)
{
GPIO_SetBits(GPIOB, GPIO_Pin_3); // LED4亮
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_3); // LED4灭
}
}
}
```
以上代码将不断循环,检查每个按键的状态,并相应地控制每个LED的状态。如果按键被按下,则相应的LED将亮起,否则将熄灭。
相关推荐
![](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)