stm32定時(shí)器捕捉高低電平計(jì)時(shí)長代碼分析
本章內(nèi)容說一說關(guān)于stm32的定時(shí)器TIM_GetCounter(TIMx)的運(yùn)用。之前的項(xiàng)目中曾經(jīng)用這個(gè)來獲取高低電平的時(shí)長,在超聲波測距,紅外遙控解碼中運(yùn)用過。Stm32的定時(shí)器除了1和8其他都是通用定時(shí)器。如何用這句函數(shù)來捕捉電平時(shí)長呢?首先看一下初始化內(nèi)容,以TIM4為例
本文引用地址:http://2s4d.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時(shí)鐘,經(jīng)過分頻后產(chǎn)生10KHZ 100us周期的波形。TIM->CNT這個(gè)是計(jì)數(shù)
。。。。。
評(píng)論