(3)定時(shí)器的捕獲功能配置流程如下:流程精簡(jiǎn):1、IC配置(設(shè)置捕獲模式,通道、使能捕獲中斷)
本文引用地址:http://butianyuan.cn/article/201611/321630.htm2、中斷向量的嵌套,以及對(duì)應(yīng)的PIO口配置
3、中斷服務(wù)函數(shù)的配置
1、IC配置(設(shè)置捕獲模式,通道、使能捕獲中斷)
voidTIM3_Configuration(void) //定時(shí)器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;//管教與寄存器的對(duì)應(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);//選擇時(shí)鐘觸發(fā)源
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);//觸發(fā)方式
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);//啟動(dòng)定時(shí)器的被動(dòng)觸發(fā)
TIM_Cmd(TIM3, ENABLE);//啟動(dòng)tim2
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);//打開中斷
}
2、中斷向量的嵌套,以及對(duì)應(yīng)的PIO口配置
void NVIC_Configuration(void)
{NVIC_InitTypeDef NVIC_InitStructure;
//為EXTI_Line9使能并配置中斷向量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置優(yōu)先級(jí)分組:先占優(yōu)先級(jí)2位,從優(yōu)先級(jí)2位
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//搶占優(yōu)先級(jí)0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//響應(yīng)優(yōu)先級(jí)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);//中斷的重點(diǎn),進(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);
}
}
評(píng)論