基于标准库初始化PE0按键下降沿外部中断读取键值的代码不用STM32CubeMX生成
时间: 2024-01-24 18:02:00 浏览: 22
好的,以下是基于标准库的PE0按键下降沿外部中断读取键值的代码:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_rcc.h"
void EXTI0_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line0) != RESET) //检查是否发生外部中断
{
//执行按键操作
//...
EXTI_ClearFlag(EXTI_Line0); //清除中断标志位
}
}
void GPIO_Configuration(void)
{
//使能GPIOE时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
//配置PE0为输入模式
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
//使能AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//配置EXTI线路0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource0);
EXTI_InitTypeDef EXTI_InitStructure;
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_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
GPIO_Configuration();
EXTI_Configuration();
while (1)
{
//主循环
}
}
```
在上述代码中,我们首先通过`GPIO_Configuration()`函数配置PE0为输入模式,并且上拉输入。然后,在`EXTI_Configuration()`函数中配置EXTI线路0,使其在下降沿触发中断,并且使能中断。在`EXTI0_IRQHandler()`中断服务函数中,我们可以执行按键操作,并且清除中断标志位。最后,在主循环中我们可以执行其他任务。