(3)
定时器的捕获功能配置流程如下:
流程精简:1、IC配置(设置捕获模式,通道、使能捕获中断)
2、中断向量的嵌套,以及对应的PIO口配置
3、中断服务函数的配置
1、IC配置(设置捕获模式,通道、使能捕获中断)
voidTIM3_Configuration(void) //定时器3初始化;输入捕获设置
{TIM_ICInitTypeDefTIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//通道选择
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿触发
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//管教与寄存器的对应关系
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_div1; //分频器
TIM_ICInitStructure.TIM_ICFilter = 0x0;//滤波设置
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);//PWM输入模式初始化
//TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);//选择时钟触发源
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);//触发方式
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);//启动定时器的被动触发
TIM_Cmd(TIM3, ENABLE);//启动tim2
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);//打开中断
}
2、中断向量的嵌套,以及对应的PIO口配置
void NVIC_Configuration(void)
{NVIC_InitTypeDef NVIC_InitStructure;
//为EXTI_Line9使能并配置中断向量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置优先级分组:先占优先级2位,从优先级2位
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//响应优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//允许中断
NVIC_Init(&NVIC_InitStructure); //写入设置}
3、中断服务函数的配置
void TIM3_IRQHandler(void)
{
LCD12864_Put_Num(4,0,132454);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);//中断的重点,进入后要清除相应的标志位以跳出中断
i++;
IC2Value=TIM_GetCapture2(TIM3);//捕获的是周期
IC1Value=TIM_GetCapture1(TIM3);//捕获的是高电平
if(i==500)
{i=0;
LCD12864_Put_Num(1,0,72000000/IC2Value);
LCD12864_Put_Num(2,0,72000000/IC1Value);
}
}