配置流程
需要对AFIO、EXTI、NVIC、GPIOB进行配置形成通路将中断连接至CPU
APB2总线连接的寄存器
LED灯代码
#include "stm32f10x.h" // Device header
#include "Delay.h"GPIO_InitTypeDef GIPO_InitStruct;//结构体配置GPIO
EXTI_InitTypeDef EXTI_Int_Struct;//结构体Exti
NVIC_InitTypeDef NVIC_IniyStruct;GPIO_InitTypeDef GPIO_InitStructure;//定义变量GPIOA结构体void CountSensor_Init(void){//配置中断/*开启APB2的时钟*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//开启APB2的外设,GPIOB是APB2的外设RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//开启AFIO的外设,AFIO是APB2的外设//EXTI和NVIC外设默认打开不必打开/*配置GPIO*/
GIPO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入模式input、out
GIPO_InitStruct.GPIO_Pin = GPIO_Pin_14;//开启14号引脚
GIPO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GIPO_InitStruct);/*AFIO外外部引脚选择*/GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);//PB14连接
EXTI_Int_Struct.EXTI_Line = EXTI_Line14;//接14
EXTI_Int_Struct.EXTI_LineCmd = ENABLE;//开启
EXTI_Int_Struct.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式
EXTI_Int_Struct.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发EXTI_Init(&EXTI_Int_Struct);//写入/*将EXTI的14号线路配置为中断模式,下降沿触发,连接在PB14端口*///配置NvicNVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置响应等级NVIC_IniyStruct.NVIC_IRQChannel = EXTI15_10_IRQn;//找到对应通道
NVIC_IniyStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_IniyStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_IniyStruct.NVIC_IRQChannelSubPriority = 1;
//抢占优先级和响应优先级都设置为1NVIC_Init(&NVIC_IniyStruct);
}//写中断函数void GPIOA_INIT(void){//初始化GPIOA代码GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//零号引脚GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出50HZGPIO_Init(GPIOA, &GPIO_InitStructure);//写入GPIO_SetBits(GPIOA, GPIO_Pin_0);
}void LED_LIGHT(void){//点亮灯代码GPIO_ResetBits(GPIOA, GPIO_Pin_0);//指定端口设置为低电平
}int count = 0;void EXTI15_10_IRQHandler(void){//名字得写对(10~15)if(EXTI_GetITStatus(EXTI_Line14) == SET){//查看是否是对应的中断函数EXTI_ClearITPendingBit(EXTI_Line14);//清除GPIO_ResetBits(GPIOA, GPIO_Pin_0);Delay_ms(150);GPIO_SetBits(GPIOA,GPIO_Pin_0);}}int main(void){CountSensor_Init();//初始化中断RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//开启PA端口时钟GPIOA_INIT();//初始化GPIOA代码while(1){}}
LED灯效果
蜂鸣器代码
#include "stm32f10x.h" // Device header
#include "Delay.h"GPIO_InitTypeDef GIPO_InitStruct;//结构体配置GPIO
EXTI_InitTypeDef EXTI_Int_Struct;//结构体Exti
NVIC_InitTypeDef NVIC_IniyStruct;GPIO_InitTypeDef GPIO_InitStructure;//定义变量GPIOA结构体void CountSensor_Init(void){//配置中断/*开启APB2的时钟*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//开启APB2的外设,GPIOB是APB2的外设RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//开启AFIO的外设,AFIO是APB2的外设//EXTI和NVIC外设默认打开不必打开/*配置GPIO*/
GIPO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉输入模式input、out
GIPO_InitStruct.GPIO_Pin = GPIO_Pin_14;//开启14号引脚
GIPO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GIPO_InitStruct);/*AFIO外外部引脚选择*/GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);//PB14连接
EXTI_Int_Struct.EXTI_Line = EXTI_Line14;//接14
EXTI_Int_Struct.EXTI_LineCmd = ENABLE;//开启
EXTI_Int_Struct.EXTI_Mode = EXTI_Mode_Interrupt;//中断模式
EXTI_Int_Struct.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发EXTI_Init(&EXTI_Int_Struct);//写入/*将EXTI的14号线路配置为中断模式,下降沿触发,连接在PB14端口*///配置NvicNVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置响应等级NVIC_IniyStruct.NVIC_IRQChannel = EXTI15_10_IRQn;//找到对应通道
NVIC_IniyStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_IniyStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_IniyStruct.NVIC_IRQChannelSubPriority = 1;
//抢占优先级和响应优先级都设置为1NVIC_Init(&NVIC_IniyStruct);
}//写中断函数void GPIOA_INIT(void){//初始化GPIOA代码GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//零号引脚GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出50HZGPIO_Init(GPIOA, &GPIO_InitStructure);//写入//GPIO_SetBits(GPIOA, GPIO_Pin_0);
}void LED_LIGHT(void){//点亮灯代码GPIO_ResetBits(GPIOA, GPIO_Pin_0);//指定端口设置为低电平
}int count = 0;void EXTI15_10_IRQHandler(void){//名字得写对(10~15)if(EXTI_GetITStatus(EXTI_Line14) == SET){//查看是否是对应的中断函数EXTI_ClearITPendingBit(EXTI_Line14);//清除GPIO_SetBits(GPIOA,GPIO_Pin_0);Delay_ms(150);GPIO_ResetBits(GPIOA, GPIO_Pin_0);}}int main(void){CountSensor_Init();//初始化中断RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//开启PA端口时钟GPIOA_INIT();//初始化GPIOA代码while(1){}}
效果
注:GND接负极、VCC正级、I/O接引脚、正电平触发、设置初始电平为0、按下按键电平转1