STM32f103最小系统板
PC13 接一个LED小灯
首先配置LED灯
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//初始化PC13为推挽式输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
然后配置按键键盘为上拉式输入和配置EXTI函数
void EXTI_KEY_Init(void)//配置PB11为下降沿触发
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//配置管脚时钟和复用时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
//配置GPIO为上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//配置GPIO口为外部中断模式
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource10);
//配置EXTI_LINE10中断触发模式
EXTI_InitStructure.EXTI_Line = EXTI_Line10;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//配置EXTI的中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line10) == 1) //判断哪个按键输入
{
LED = 0;
EXTI_ClearFlag(EXTI_Line10);
}
if(EXTI_GetFlagStatus(EXTI_Line11) == 1) //判断哪个按键输入
{
LED = 1;
EXTI_ClearFlag(EXTI_Line11);
}
}
在主函数里初始化LED EXTI函数
int main(void)
{
NVIC_Configuration();
LED_Init();
EXTI_KEY_Init();
delay_init();
while(1)
{
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b64851e74547cfa6ca26d840b39d64e6.jpeg)