stm32按键控制led
时间: 2023-09-18 14:11:42 浏览: 38
要实现按键控制LED,需要配置GPIO口作为输入和输出,并在程序中编写按键检测和LED控制的代码。
以下是一个简单的示例代码:
```c
#include "stm32f10x.h"
#define LED_PIN GPIO_Pin_13
#define LED_PORT GPIOC
#define KEY_PIN GPIO_Pin_0
#define KEY_PORT GPIOA
#define KEY_EXTI_LINE EXTI_Line0
#define KEY_NVIC_IRQChannel EXTI0_IRQn
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
GPIO_Configuration();
EXTI_Configuration();
NVIC_Configuration();
while (1)
{
if (GPIO_ReadInputDataBit(KEY_PORT, KEY_PIN) == 0)
{
GPIO_WriteBit(LED_PORT, LED_PIN, Bit_RESET);
}
else
{
GPIO_WriteBit(LED_PORT, LED_PIN, Bit_SET);
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
// LED
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
// KEY
GPIO_InitStructure.GPIO_Pin = KEY_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(KEY_PORT, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = KEY_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = KEY_NVIC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(KEY_EXTI_LINE) != RESET)
{
if (GPIO_ReadInputDataBit(KEY_PORT, KEY_PIN) == 0)
{
GPIO_WriteBit(LED_PORT, LED_PIN, Bit_RESET);
}
else
{
GPIO_WriteBit(LED_PORT, LED_PIN, Bit_SET);
}
EXTI_ClearITPendingBit(KEY_EXTI_LINE);
}
}
```
在这个示例代码中,我们使用PA0作为按键输入口,PC13作为LED的输出口。在GPIO配置函数中,我们将PA0配置为上拉输入,PC13配置为推挽输出。在主函数的while循环中,我们读取PA0的输入状态来控制PC13的输出状态。同时,我们在中断服务函数中检测按键的状态并执行相应的操作。需要注意的是,我们使用了外部中断来检测按键的状态,因此需要在相应的函数中配置外部中断。
相关推荐
![](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)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)