void TIM8_Encoder_Init(void)
{
GPIO_InitTypeDef GPIO_InitTypestruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypestruct;
TIM_ICInitTypeDef TIM_ICInitTypestruct;
NVIC_InitTypeDef NVIC_InitTypestruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_TIM8,ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitTypestruct.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitTypestruct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitTypestruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitTypestruct);
TIM_TimeBaseInitTypestruct.TIM_Period=3999;
TIM_TimeBaseInitTypestruct.TIM_Prescaler=0;
TIM_TimeBaseInitTypestruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitTypestruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitTypestruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitTypestruct);
TIM_EncoderInterfaceConfig(TIM8,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
// TIM_ICStructInit(&TIM_ICInitTypestruct);
TIM_ICInitTypestruct.TIM_Channel=TIM_Channel_1;
TIM_ICInitTypestruct.TIM_ICFilter=15;
// TIM_ICInitTypestruct.TIM_ICPolarity=TIM_ICPolarity_BothEdge;
TIM_ICInitTypestruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitTypestruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitTypestruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM8,&TIM_ICInitTypestruct);
// TIM_ICInitTypestruct.TIM_Channel=TIM_Channel_2;
// TIM_ICInitTypestruct.TIM_ICFilter=15;
TIM_ICInitTypestruct.TIM_ICPolarity=TIM_ICPolarity_BothEdge;
// TIM_ICInitTypestruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
// TIM_ICInitTypestruct.TIM_ICPrescaler=TIM_ICPSC_DIV1;
// TIM_ICInitTypestruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
// TIM_ICInit(TIM8,&TIM_ICInitTypestruct);
// TIM8->CR1&=~(3<<5);
// TIM8->CCMR1|=1<<0;
// TIM8->CCMR1|=1<<8;
// TIM8->CCER&=~(2<<0);
// TIM8->CCER&=~(1<<5);
NVIC_InitTypestruct.NVIC_IRQChannel=TIM8_UP_IRQn;
NVIC_InitTypestruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitTypestruct.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitTypestruct.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitTypestruct);
TIM_ARRPreloadConfig(TIM8,ENABLE);
TIM_ClearFlag(TIM8,TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM8,TIM_IT_Update);
TIM_ITConfig(TIM8,TIM_IT_Update,ENABLE);
TIM_SetCounter(TIM8,0);
TIM_Cmd(TIM8,ENABLE);
}
代码如下:
|