本帖最后由 sisyphos 于 2014-3-22 15:32 编辑
定时器1 channel4,采用输入捕获模式
初始化如下:
void Tim1Init(void)
{
//端口PA11初始化
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); //使能PA端口时钟
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = SIGNAL_CAPTURE_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //悬空输入
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SIGNAL_CAPTURE_PORT,&GPIO_InitStruct);
//Tim1,AF功能配置
GPIO_PinAFConfig(SIGNAL_CAPTURE_PORT,SIGNAL_CAPTURE_PIN,GPIO_AF_2);
//Tim1初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); // 使能时钟单元
TIM_PrescalerConfig(TIM1, TIM1_CLK_DIV_NUM, TIM_PSCReloadMode_Immediate);
TIM_SetAutoreload(TIM1, TIM1_WORK_RELOAD_NUM); //
TIM_ICInitTypeDef TIM1_ICInitStruct;
TIM1_ICInitStruct.TIM_Channel = TIM_Channel_4;
TIM1_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM1_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM1_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM1_ICInitStruct.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1,&TIM1_ICInitStruct);
TIM1->SR = 0;
TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能定时器
}
void TIM1_CC_IRQHandler(void)
{
INT32 tempccr = 0;
INT32 capturedata = 0;
INT32 err;
if (TIM_GetITStatus(TIM1,TIM_IT_CC4))
{
TIM_ClearFlag(TIM1, TIM_IT_CC4);
tempccr = TIM_GetCapture4(TIM1); //获取当前捕获的计数值
}
}
static void _system_IntSet(void)
{
__set_PRIMASK(1);//关中断
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1_BRK_UP_TRG_COM_IRQn, TIM1_CC_IRQn
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
|