include "stm32f10x.h" // Device header
extern uint16_t NUM;
int flag = 0,times;
void IC_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启时钟
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//输入模式不需要设置速率
GPIO_Init(GPIOB,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource6);//选择中断的GPIOx 和线路 数字要与输入的GPIO口的数字对应
EXTI_InitTypeDef InitStructure;
InitStructure.EXTI_Line = EXTI_Line6; //与GPIO口的数字对应
InitStructure.EXTI_LineCmd = ENABLE;//开启中断
InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断响应事件 为中断事件
InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//边沿触发方式
EXTI_Init(&InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//优先级分配
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//配置NVIC通道
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;//使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;//响应优先级
NVIC_Init(&NVIC_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_InternalClockConfig(TIM3);//选择内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//TIM_CKD_DIVx 代表x 72M要被分为多少个
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInitStruct.TIM_Period = 1000-1;//TIM_Prescaler装1000次 与CNT CCR同级
TIM_TimeBaseInitStruct.TIM_Prescaler = 72-1;//每产生义一个tips加1,加到72归零,TIM_Period+1
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;//高级定时器使用 这里给0
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//时钟触发中断
NVIC_InitTypeDef NVIC_InitStructure2;
NVIC_InitStructure2.NVIC_IRQChannel = TIM3_IRQn;//来自TIM3的中断
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级1
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1;//响应优先级1
NVIC_Init(&NVIC_InitStructure2);
/***********IC********************************/
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
{
NUM++;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
void EXTI9_5_IRQHandler(void)
{
if(SET == EXTI_GetITStatus(EXTI_Line6))
{
if(flag == 0)
{
NUM=0;flag=1;//0为第一个双边沿,1为第二个双边沿
TIM_SetCounter(TIM3,0);//设置TIM3的CNT为0
TIM_Cmd(TIM3,ENABLE);//开始计时
}
else
{
TIM_Cmd(TIM3,DISABLE);
flag = 0;
times = NUM*1000 +TIM_GetCounter(TIM3);
}
EXTI_ClearITPendingBit(EXTI_Line6);//清空中断标志位
}
}