(3)定時器的捕獲功能配置流程如下:流程精簡:1、IC配置(設(shè)置捕獲模式,通道、使能捕獲中斷)
本文引用地址:http://m.butianyuan.cn/article/201611/321630.htm2、中斷向量的嵌套,以及對應(yīng)的PIO口配置
3、中斷服務(wù)函數(shù)的配置
1、IC配置(設(shè)置捕獲模式,通道、使能捕獲中斷)
voidTIM3_Configuration(void) //定時器3初始化;輸入捕獲設(shè)置
{TIM_ICInitTypeDefTIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//通道選擇
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿觸發(fā)
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//管教與寄存器的對應(yīng)關(guān)系
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //分頻器
TIM_ICInitStructure.TIM_ICFilter = 0x0;//濾波設(shè)置
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);//PWM輸入模式初始化
//TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);//選擇時鐘觸發(fā)源
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);//觸發(fā)方式
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);//啟動定時器的被動觸發(fā)
TIM_Cmd(TIM3, ENABLE);//啟動tim2
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);//打開中斷
}
2、中斷向量的嵌套,以及對應(yīng)的PIO口配置
void NVIC_Configuration(void)
{NVIC_InitTypeDef NVIC_InitStructure;
//為EXTI_Line9使能并配置中斷向量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置優(yōu)先級分組:先占優(yōu)先級2位,從優(yōu)先級2位
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//搶占優(yōu)先級0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//響應(yīng)優(yōu)先級1
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//允許中斷
NVIC_Init(&NVIC_InitStructure); //寫入設(shè)置}
3、中斷服務(wù)函數(shù)的配置
void TIM3_IRQHandler(void)
{
LCD12864_Put_Num(4,0,132454);
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);//中斷的重點,進(jìn)入后要清除相應(yīng)的標(biāo)志位以跳出中斷
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);
}
}
評論