环境:
主机:WIN7
开发环境:MDK4.23
MCU:STM32F103CBT6
说明:
STM32有20个外部中断线,其中EXTI0-EXTI15给I/O端口使用
EXTI线16连接到PVD输出
EXTI线17连接到RTC闹钟事件
EXTI线18连接到USB唤醒事件
EXTI线19连接到以太网唤醒事件(只适用于互联型产品)
源代码:
初始化:
- //打开NTRX外部中断
- void open_ntrx_irq(void)
- {
- //定义中断结构体
- NVIC_InitTypeDef NVIC_InitStructure ;
- //定义外部中断结构体
- EXTI_InitTypeDef EXTI_InitStructure;
- //定义IO初始化结构体
- GPIO_InitTypeDef GPIO_InitStructure;
- //初始化NTRX中断脚PB1时钟
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置为输入
- GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOB初始化
- //配置中断源为PB1
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
- // 配置EXTI_Line1下降沿触发
- EXTI_ClearITPendingBit(EXTI_Line1);
- EXTI_InitStructure.EXTI_Line = EXTI_Line1;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- //打开NTRX中断
- NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //通道设置为外部中断线0
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f; //中断占先等级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f; //中断响应优先级0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
- NVIC_Init(&NVIC_InitStructure); //初始化
- //EXTI_Line1中断允许
- EXTI_GenerateSWInterrupt(EXTI_Line1);
- }
//打开NTRX外部中断
void open_ntrx_irq(void)
{
//定义中断结构体
NVIC_InitTypeDef NVIC_InitStructure ;
//定义外部中断结构体
EXTI_InitTypeDef EXTI_InitStructure;
//定义IO初始化结构体
GPIO_InitTypeDef GPIO_InitStructure;
//初始化NTRX中断脚PB1时钟
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置为输入
GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIOB初始化
//配置中断源为PB1
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
// 配置EXTI_Line1下降沿触发
EXTI_ClearITPendingBit(EXTI_Line1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//打开NTRX中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //通道设置为外部中断线0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
//EXTI_Line1中断允许
EXTI_GenerateSWInterrupt(EXTI_Line1);
}
中断函数:
- void EXTI1_IRQHandler(void)
- {
- if(EXTI_GetITStatus(EXTI_Line1) != RESET)
- {
- //清中断
- EXTI_ClearITPendingBit(EXTI_Line1);
- //中断标志置位
- Flag_IRQ.ntrx = 1;
- }
- }