外部中断
#include "stm32f10x.h" // Device header
uint16_t shi_jian_cha[3];
//外部中断线路的初始化
void CountSensor_Init(void)
{
//开启APB2的外围设备GPIOB、AFIO(中断引脚选择)的时钟,NVIC和EXIT不需要开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//用GPIO初始化结构,来初始化PB0,PB1,PB3
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//无输入时底电平
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//选择用作EXTI(边沿检测及控制)线的引脚
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);//触发线选择PB0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);//触发线选择PB1
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);//触发线选择PA3
//用EXTI初始化结构,来初始化EXTI
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line3;//外部中断线选择0,1,3
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//启动外部中断
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//触发方式
EXTI_Init(&EXTI_InitStructure);
//配置优先级分组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//用NVIC初始化结构,来初始化NVIC(中断控制器)
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//中断通道选择外部中断线路0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//指定抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//指定响应优先级为1
NVIC_Init(&NVIC_InitStructure);
//用NVIC初始化结构,来初始化NVIC(中断控制器)
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;//中断通道选择外部中断线路1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//指定抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;//指定响应优先级为2
NVIC_Init(&NVIC_InitStructure);
//用NVIC初始化结构,来初始化NVIC(中断控制器)
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//中断通道选择外部中断线路3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//指定抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//指定响应优先级为3
NVIC_Init(&NVIC_InitStructure);
}
/*
uint16_t TIM_GetCounter(TIM_TypeDef* TIMx);//获取TIM的计数值
void TIM_DeInit(TIM_TypeDef* TIMx)//反初始化为默认初始值
*/
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) == SET)//判断外部中断线路12是否置位
{
//获取TIM的计数值存入shi_jian_cha数组
shi_jian_cha[0]=TIM_GetCounter(TIM2);
EXTI_ClearITPendingBit(EXTI_Line0);//清除中断标志位
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) == SET)
{
//获取TIM的计数值存入shi_jian_cha数组
shi_jian_cha[1]=TIM_GetCounter(TIM2);
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI3_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line3) == SET)
{
//获取TIM的计数值存入shi_jian_cha数组
shi_jian_cha[2]=TIM_GetCounter(TIM2);
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
|