中断可以理解为一个有激励给它就会立马给你响应的一个东东。就是说在你执行main函数的内容时,当有这个触发时便会跳转到这个中断内存,执行这段代码。
Attention:
1、中断的用法,可以使用中断作为第二main函数,但是前提是必须中断代码的时间复杂度要远远低于main,才可以这样做。
2、中断也分优先级,也有中断的嵌套,这个需要根据相关的芯片去查对应的数据手册即可。
#include "stm32f10x.h" unsigned char i=0; int main() { GPIO_InitTypeDef GPIO_InitStructure; //配置IO口结构体 NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOA, &GPIO_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource15); EXTI_InitStructure.EXTI_Line=EXTI_Line15;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd=ENABLE; EXTI_Init(&EXTI_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_0); while(1) { } } void EXTI15_10_IRQHandler() { if(EXTI_GetITStatus(EXTI_Line15)!=RESET) { GPIO_ResetBits(GPIOA, GPIO_Pin_0); EXTI_ClearITPendingBit(EXTI_Line15); } }
由于备注,我实在是受不了乱码问题,所以直接附图。
#include "stm32f10x.h" unsigned char i=0; int main() { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //ÕâÒ»ÐÐÖеÄËٶȻù±¾ÎÞÓ°Ïì GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOA, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_TimeBaseStructure.TIM_Period=10000; TIM_TimeBaseStructure.TIM_Prescaler=7199; TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE); TIM_Cmd(TIM3, ENABLE); GPIO_SetBits(GPIOA, GPIO_Pin_0); while(1) { } } void TIM3_IRQHandler() { if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_Update); { i=1; GPIO_SetBits(GPIOA,GPIO_Pin_0); } else if(i==1) { i=0; GPIO_ResetBits(GPIOA,GPIO_Pin_0); } } }