1中断:每一个中断都会对应一个服务程序
2NVIC 他的做用是负责中断优先级管理
3IP bit[7:4]每一个中断都有一个IP寄存器,用于设置中断相关的属性
AIRCR[10:8]只一个AIRCR寄存器负责所有的中断的优先级的分组
4中断分组 1.void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
NVIC_PriorityGroup_0~4
5中断初始化
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
uint8_t NVIC_IRQChannel; 中断号
stm32f4xx.h typedef enum IRQn 中断号的定义
6 抢占优先级 uint8_t NVIC_IRQChannelPreemptionPriority; 0~15
7 响应优先级 NVIC_IRQChannelSubPriority; 0~15
8中断使能开关 FunctionalState NVIC_IRQChannelCmd; ENABLE DISABLE
9 中断响应方式 电平触发
边沿触发
跳变触发 上升沿 下降沿 双边沿
10选择中断线: EXTI 0~15 可选择对应的GPIO端口
void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex)
每一个EXTIn 只能是每组GPIO第
每一个EXTIn 只能是每组GPIO第n个端口
EXTI_PortSourceGPIOx EXTI_PortSourceGPIOA~G 选择对应的组
11 配置触发方式
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
uint32_t EXTI_Line; 外部中断线
EXTI_Line0~22
EXTIMode_TypeDef EXTI_Mode;
EXTI_Mode_Interrupt 中断
EXTI_Mode_Event 事件
EXTITrigger_TypeDef EXTI_Trigger 触发方式
EXTI_Trigger_Rising 上升沿
EXTI_Trigger_Falling 下降沿
EXTI_Trigger_Rising_Falling 双边沿
EXTI_LineCmd 使能开关
ENABLE DISABLE
代码示例:
1 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
2
NVIC_InitTypeDef NVIC_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStruct);
2 看门狗
1打开写保护 void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess);
2设置分频系数 void IWDG_SetPrescaler(uint8_t IWDG_Prescaler);
3设置RLR寄存器值 void IWDG_SetReload(uint16_t Reload);
4喂狗 void IWDG_ReloadCounter(void);
5使能看门狗 void IWDG_Enable(void);
6手动喂狗 IWDG_ReloadCounter();
void iwdg_init()
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(IWDG_Prescaler_64);
//64/32000 * 1000
IWDG_SetReload(1000);
IWDG_ReloadCounter(); //
IWDG_Enable();
}
//中断服务程序,名字固定,并且类型void (void)
void EXTI0_IRQHandler(void)
{
//1.判断该中断产生源
delay_ms(10);//消除抖动
if(0 == WK_UP) { //判断中断确实由WK_UP按键产生
//2.中断处理部分
//BEEP = !BEEP;
IWDG_ReloadCounter();
}
//3.清除中断标志
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(0 == KEY0) {
//2.中断处理部分
PFout(9) = !PFout(9);
}
//3.清除中断标志
EXTI_ClearITPendingBit(EXTI_Line4);
}
void EXTI3_IRQHandler(void)
{
delay_ms(10);
if(0 == KEY1) {
//2.中断处理部分
PFout(10) = 0;
} else if(1 == KEY1) {
PFout(10) = 1;
}
//3.清除中断标志
EXTI_ClearITPendingBit(EXTI_Line3);
}
void my_exti_init()
{
NVIC_InitTypeDef NVIC_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
key_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);
EXTI_InitStruct.EXTI_Line = EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStruct);
EXTI_InitStruct.EXTI_Line = EXTI_Line4;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStruct);
EXTI_InitStruct.EXTI_Line = EXTI_Line3;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStruct);
}