- UID
- 1029342
- 性别
- 男
|
下面结合外部中断附上固件库版本的程序:
主函数里:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitSructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置为优先级组2
NVIC_InitSructure.NVIC_IRQChannel = EXTI15_10_IRQn; //定义外部中断线13中断通道
NVIC_InitSructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitSructure.NVIC_IRQChannelSubPriority = 0; //响应优先级0
NVIC_InitSructure.NVIC_IRQChannelCmd = ENABLE; //使能指定通道
NVIC_Init(&NVIC_InitSructure);
NVIC_InitSructure.NVIC_IRQChannel = EXTI15_10_IRQn; //定义外部中断线15中断通道
NVIC_InitSructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitSructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitSructure.NVIC_IRQChannelCmd = ENABLE; //使能指定通道
NVIC_Init(&NVIC_InitSructure);
NVIC_InitSructure.NVIC_IRQChannel = EXTI0_IRQn; //定义外部中断线0中断通道
NVIC_InitSructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitSructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitSructure.NVIC_IRQChannelCmd = ENABLE; //使能指定通道
NVIC_Init(&NVIC_InitSructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure; //初始化结构
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource13); //指明当前哪个引脚为外部中断触发引脚
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource15);
EXTI_ClearITPendingBit(EXTI_Line13); //清除中断标志位 EXTI_Line13对应相应的中断线13
EXTI_ClearITPendingBit(EXTI_Line15);
EXTI_InitStructure.EXTI_Mode =EXTI_Mode_Interrupt; //选择中断模式请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
EXTI_InitStructure.EXTI_Line = EXTI_Line13|EXTI_Line15; // 选择待使能的外部中断线
EXTI_InitStructure.EXTI_LineCmd = ENABLE; // 定义选中线的新状态 使能
EXTI_Init(&EXTI_InitStructure); //把EXIT_InitStructure中的每一个参数按缺省值填入
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); //指明当前哪个引脚为外部中断触发引脚
EXTI_ClearITPendingBit(EXTI_Line0);
EXTI_InitStructure.EXTI_Mode =EXTI_Mode_Interrupt; //选择中断模式请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
EXTI_InitStructure.EXTI_Line = EXTI_Line0; // 选择待使能的外部中断线
EXTI_InitStructure.EXTI_LineCmd = ENABLE; // 定义选中线的新状态 使能
EXTI_Init(&EXTI_InitStructure); //把EXIT_InitStructure中的每一个参数按缺省值填入
}
stm32f10x_it.c这个文件里
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13)!=RESET)
{ GPIO_WriteBit( GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8))); //LED0翻转
EXTI_ClearITPendingBit(EXTI_Line13);
}
if(EXTI_GetITStatus(EXTI_Line15)!=RESET)
{ GPIO_WriteBit( GPIOD,GPIO_Pin_2,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2))); //LED0翻转
EXTI_ClearITPendingBit(EXTI_Line15);
}
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
{ GPIO_WriteBit( GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8))); //LED0翻转
GPIO_WriteBit( GPIOD,GPIO_Pin_2,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2))); //LED0翻转
EXTI_ClearITPendingBit(EXTI_Line0);
}
} |
|