1配置中断
1、 分配中断向量表:
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
2、 设置中断优先级:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中断优先级
3、 初始化外部中断:
/*允许EXTI4中断 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;//强占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //次优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure); //初始化中断
注意:如果我们配置的外部针脚为PA4,或PB4,或PC4,PD4等,那么采用的外部中断也必须是EXTI4,同样,如果外部中断针 脚是PA1,PB1,PC1,PD1 那么中断就要用EXTI1,其他类推。
2配置GPIO针脚作为外部中断的触发事件
1、 选择IO针脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
注意,如果的针脚是端口的4号针脚,配置的中断一定是 EXTI4
2、 配置针脚为输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
3、 初始化针脚
GPIO_Init(GPIOD,&GPIO_InitStructure);
3配置EXTI线,使中断线 和IO针脚线连接上
1、 将EXTI线连接到IO端口上
将EXTI线4连接到端口GPIOD的第4个针脚上
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4);
注意:如果配置的针脚是4号,那么参数必须是GPIO_PinSource4
如果配置的针脚是3号,那么参数必须是GPIO_PinSource3
2、配置中断边沿
/*配置EXTI线0上出现下降沿,则产生中断*/
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
注意:如果配置的4号针脚,那么EXTI_Line4是必须的
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中断线使能
EXTI_Init(&EXTI_InitStructure); //初始化中断
EXTI_GenerateSWInterrupt(EXTI_Line4); //EXTI_Line4中断允许
到此中断配置完成,可以写中断处理函数。
举例:
配置函数
/*************************************************************************
* 函数名 NVIC_Configration
* 描述 配置各个中断寄存器
* 输入 无
* 输出 无
* 返回值 无
****************************************************************************/
void NVIC_Configration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
//#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
//NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置中断优先级
/*允许EXTI4中断 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*允许EXTI9中断*/
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*配置SysTick处理优先级:优先级以及子优先级*/
}
/************************************************************************
* 函数名 :GPIO_Configuration(void)
* 描述 :配置TIM2阵脚
* 输入 :无
* 输出 :无
* 返回 :无
************************************************************************/
void GPIO_Configuration(void){
/* GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*配置GPIOD的第一个管角为浮动输入*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD,&GPIO_InitStructure);
/*配置GPIOB的第9个管脚为浮动输入*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
/**************************************************************
* 函数 SysTick_Configuration
* 描述 设置SysTick
* 输入 无
* 输出 无
* 返回值 无
***************************************************************/
void SysTick_Configuration(void)
{
/*配置 HCLK 时钟做为SysTick 时钟源*/
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //系统时钟8分频 72MHz
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 8,2);
/*SysTick Interrupt each 1000Hz with HCLK equal to 72MHz*/
SysTick_SetReload(9000);//中断周期1ms
/*Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);//打开中断
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick_CounterCmd(SysTick_Counter_Clear);
}
/******************************************************************************
* 函数名 EXTI_Configuration
* 描述 配置EXTI线
* 输入 无
* 输出 无
* 返回值 无
******************************************************************************/
void EXTI_Configuration(void){
/*将EXTI线0连接到PA0*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4);
/*配置EXTI线0上出现下降沿,则产生中断*/
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_GenerateSWInterrupt(EXTI_Line4);
/*将EXTI线9连接到PB9上*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);
/*将EXTI线9上出现下降沿产生中断*/
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_GenerateSWInterrupt(EXTI_Line9);
}
中断函数:
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4)!= RESET){
EXTI_ClearITPendingBit(EXTI_Line4);
if(Ledflag == 0){
Ledflag = 1;
GPIOC->ODR |= 0X00000080;
}
else{
Ledflag = 0;
GPIOC->ODR &= 0XFFFFFF7F;
}
}
}