江科大學(xué)習(xí)筆記 _7.定時器輸入捕獲


TIM_CCMR對輸入信號進(jìn)行采樣,連續(xù)N個信號都為高(低)電平時,才輸出高(低)電平。
TIM_CCMR能防止信號抖動,濾波
?
TI1F選擇邊沿檢測的極性:上升或下降沿,得到TL1FP1觸發(fā)信號,
?


1.?RCC時鐘開啟,GPIO和TIM
2.?GPIO初始化,配置成輸入模式(上拉或浮空)
3.?配置時基單元
4.?配置輸入捕獲單元,(濾波器、極性、通道選擇、分頻器)
5.?選擇從模式觸發(fā)源(TL1FP1)
6.?選擇觸發(fā)后執(zhí)行的操作,執(zhí)行Reset操作
7.?TIM_Cmd開啟定時器
?
?

1.?RCC時鐘開啟,GPIO和TIM
?RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
?RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
?
2.GPIO初始化,配置成輸入模式(上拉或浮空)
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
?
3.配置時基單元
????TIM_InternalClockConfig(TIM3);
????TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
????TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
????TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
????TIM_TimeBaseInitStruct.TIM_Period = 65536 - 1;//ARR
????TIM_TimeBaseInitStruct.TIM_Prescaler = 72 - 1;//PSC
????TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
????TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
?
4.配置輸入捕獲單元,(濾波器、極性、通道選擇、分頻器)
? TIM_ICInitTypeDef TIM_ICInitStruct;
????TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
????TIM_ICInitStruct.TIM_ICFilter = 0xF;
????TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
????TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
????TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
????TIM_ICInit(TIM3, &TIM_ICInitStruct);
?
5.選擇從模式觸發(fā)源(TL1FP1)
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
6.選擇觸發(fā)后執(zhí)行的操作,執(zhí)行Reset操作
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
?
uint32_t IC_GetFreq(void)
{
? ? return (1000000/(TIM_GetCapture1(TIM3)+1));
}