一.嵌套中断向量控制器NVIC
1.NVIC介绍:管理中断嵌套,管理中断优先级
向量表在汇编文件中
1)选择哪一组响应优先级;
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
@arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority * 4 bits for subpriority * @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority * 3 bits for subpriority * @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority * 2 bits for subpriority * @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority * 1 bits for subpriority * @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority * 0 bits for subpriority
2)NVIC初始化:
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
结构体指针:因此要先定义结构体;
typedef struct { uint8_t NVIC_IRQChannel; //NVIC中断通道 uint8_t NVIC_IRQChannelPreemptionPriority; //抢占优先级 最大值15 uint8_t NVIC_IRQChannelSubPriority; //响应优先级 最大值15 FunctionalState NVIC_IRQChannelCmd; //ENABLE或DISABLE 使能通道 } NVIC_InitTypeDef;
(1)uint8_t NVIC_IRQChannel; //NVIC中断通道
EXTI9_5_IRQn 为5到9通用中断
typedef enum IRQn { /****** Cortex-M3 Processor Exceptions Numbers ***************************************************/ NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */ MemoryManagement_IRQn = -12, /*!< 4 Cortex-M3 Memory Management Interrupt */ BusFault_IRQn = -11, /*!< 5 Cortex-M3 Bus Fault Interrupt */ UsageFault_IRQn = -10, /*!< 6 Cortex-M3 Usage Fault Interrupt */ SVCall_IRQn = -5, /*!< 11 Cortex-M3 SV Call Interrupt */ DebugMonitor_IRQn = -4, /*!< 12 Cortex-M3 Debug Monitor Interrupt */ PendSV_IRQn = -2, /*!< 14 Cortex-M3 Pend SV Interrupt */ SysTick_IRQn = -1, /*!< 15 Cortex-M3 System Tick Interrupt */ /****** STM32 specific Interrupt Numbers *********************************************************/ WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */ PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */ TAMPER_IRQn = 2, /*!< Tamper Interrupt */ RTC_IRQn = 3, /*!< RTC global Interrupt */ FLASH_IRQn = 4, /*!< FLASH global Interrupt */ RCC_IRQn = 5, /*!< RCC global Interrupt */ EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */ EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */ EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */ EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */ EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */ DMA1_Channel1_IRQn = 11, /*!< DMA1 Channel 1 global Interrupt */ DMA1_Channel2_IRQn = 12, /*!< DMA1 Channel 2 global Interrupt */ DMA1_Channel3_IRQn = 13, /*!< DMA1 Channel 3 global Interrupt */ DMA1_Channel4_IRQn = 14, /*!< DMA1 Channel 4 global Interrupt */ DMA1_Channel5_IRQn = 15, /*!< DMA1 Channel 5 global Interrupt */ DMA1_Channel6_IRQn = 16, /*!< DMA1 Channel 6 global Interrupt */ DMA1_Channel7_IRQn = 17, /*!< DMA1 Channel 7 global Interrupt */ #ifdef STM32F10X_LD ADC1_2_IRQn = 18, /*!< ADC1 and ADC2 global Interrupt */ USB_HP_CAN1_TX_IRQn = 19, /*!< USB Device High Priority or CAN1 TX Interrupts */ USB_LP_CAN1_RX0_IRQn = 20, /*!< USB Device Low Priority or CAN1 RX0 Interrupts */ CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */ CAN1_SCE_IRQn = 22, /*!< CAN1 SCE Interrupt */ EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */ TIM1_BRK_IRQn = 24, /*!< TIM1 Break Interrupt */ TIM1_UP_IRQn = 25, /*!< TIM1 Update Interrupt */ TIM1_TRG_COM_IRQn = 26, /*!< TIM1 Trigger and Commutation Interrupt */ TIM1_CC_IRQn = 27, /*!< TIM1 Capture Compare Interrupt */ TIM2_IRQn = 28, /*!< TIM2 global Interrupt */ TIM3_IRQn = 29, /*!< TIM3 global Interrupt */ I2C1_EV_IRQn = 31, /*!< I2C1 Event Interrupt */ I2C1_ER_IRQn = 32, /*!< I2C1 Error Interrupt */ SPI1_IRQn = 35, /*!< SPI1 global Interrupt */ USART1_IRQn = 37, /*!< USART1 global Interrupt */ USART2_IRQn = 38, /*!< USART2 global Interrupt */ EXTI15_10_IRQn = 40, /*!< External Line[15:10] Interrupts */ RTCAlarm_IRQn = 41, /*!< RTC Alarm through EXTI Line Interrupt */ USBWakeUp_IRQn = 42 /*!< USB Device WakeUp from suspend through EXTI #endif /* STM32F10X_LD */ ......
二.外部中断
外部中断线路映像:
EXTI相关数据结构与函数说明:
初始化函数:
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
typedef struct { uint32_t EXTI_Line; //外部中断线 EXTIMode_TypeDef EXTI_Mode; // 外部中断模式 //EXTI_Mode_Interrupt = 0x00 //中断模式 //EXTI_Mode_Event = 0x04 //事件模式 EXTITrigger_TypeDef EXTI_Trigger; //触发方式 //EXTI_Trigger_Rising = 0x08, 上升沿 //EXTI_Trigger_Falling = 0x0C, 下降沿 //EXTI_Trigger_Rising_Falling = 0x10 双边沿 FunctionalState EXTI_LineCmd; //中断使能 //ENABLE }EXTI_InitTypeDef;
EXTI_GetITStatus(EXTI_Line5) //返回中断状态 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6); //中断线映射,每个引脚都要加入 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7); TIM_GetITStatus(TIM3,TIM_IT_Update) //定时器中断 EXTI_ClearITPendingBit(EXTI_Line5) //清空中断标志位
TStatus EXTI_GetITStatus(uint32_t EXTI_Line) { ITStatus bitstatus = RESET; uint32_t enablestatus = 0; /* Check the parameters */ assert_param(IS_GET_EXTI_LINE(EXTI_Line)); enablestatus = EXTI->IMR & EXTI_Line; if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET)) { bitstatus = SET; } else { bitstatus = RESET; } return bitstatus; } //产生中断返回SET,不产生中断返回RESET
中断回调函数比较特殊,名字不能改用的哪一个NVIC中断通道 (不是外部中断线)寻找函数名;
TIM_GetITStatus(TIM3,TIM_IT_Update) //定时器中断 TIM_ClearITPendingBit(TIM3,TIM_IT_Update);清空中断标志位 EXTI_GetITStatus(EXTI_Line5) //返回中断状态 ,产生中断返回SET,不产生中断返回RESET EXTI_ClearITPendingBit(EXTI_Line5) //清空中断标志位
/一 GPIO初始化 GPIO_InitTypeDef GPIO_InitStructure; //2 打开时钟有两个 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE); GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); //二 中断优先级初始化 NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE; NVIC_Init(&NVIC_InitStructure); //三 外部中断初始化 EXTI_InitTypeDef EXTI_Init_Structrue; EXTI_Init_Structrue.EXTI_Line = EXTI_Line5|EXTI_Line6|EXTI_Line7; EXTI_Init_Structrue.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_Init_Structrue.EXTI_Trigger =EXTI_Trigger_Falling; //EXTI_Trigger_Rising = 0x08, 上升沿 //EXTI_Trigger_Falling = 0x0C, 下降沿 EXTI_Init_Structrue.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_Init_Structrue); //四 中断线映射 GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7);
三.DMA:直接存储器存取