/*PWM 脉冲捕获 */
void TIM2_IN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Configure PA.00 as pwm input */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStruct); // PA0,输入时不用设置速率
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_2);
/* Time 定时器基础设置 */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 47;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //选择通道1
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //输入上升沿捕获
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //配置通道与寄存器直接对应
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入捕获预分频值
TIM_ICInitStructure.TIM_ICFilter = 0x0; //输入滤波器带宽设置
TIM_ICInit(TIM2, &TIM_ICInitStructure); //输入通道配置
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); //输入触发源选择
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //从模式选择
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); //主从模式选择
TIM_Cmd(TIM2, ENABLE); //使能TIM2计数器
TIM_ITConfig(TIM2, TIM_IT_Trigger, ENABLE); //使能CC1中断请求
}
这个是我 051 的配置,你参考一下 |