stm32定時器捕捉高低電平計時長代碼分析
本章內容說一說關于stm32的定時器TIM_GetCounter(TIMx)的運用。之前的項目中曾經(jīng)用這個來獲取高低電平的時長,在超聲波測距,紅外遙控解碼中運用過。Stm32的定時器除了1和8其他都是通用定時器。如何用這句函數(shù)來捕捉電平時長呢?首先看一下初始化內容,以TIM4為例
本文引用地址:http://www.ex-cimer.com/article/201611/322307.htmvoid Timer4_CFG()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = IR_LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(IR_LED_PORT, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 3599; //TIM_CKD_DIV1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseStructure.TIM_Period = 200; // ARR
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM4,35,TIM_PSCReloadMode_Immediate);//36MHz/(3599+1)=10 000 HZ 100uS
TIM_ARRPreloadConfig(TIM4,DISABLE);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM4,TIM_IT_Trigger,ENABLE);
TIM_Cmd(TIM4, DISABLE);
}
APB1是36M時鐘,經(jīng)過分頻后產(chǎn)生10KHZ 100us周期的波形。TIM->CNT這個是計數(shù)
。。。。。
評論