---PC6接A相? PC7接B相
---目前遇到的問題是,無論編碼器正轉(zhuǎn)還是反轉(zhuǎn),計數(shù)值都是從65535向下計數(shù)!??!
下面是配置代碼,(RCC使能已開)
// TIM8 Encoder? PC6 AND PC7
? ? TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
? ? TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF;
? ? TIM_TimeBaseInitStructure.TIM_Prescaler = 1;
? ? TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
? ? TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
? ? TIM_TimeBaseInitStructure.TIM_RepetitionCounter =? 0x00;
? ? TIM_TimeBaseInit(TIM8, &TIM_TimeBaseInitStructure);
? ? TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
? ? TIM_ICStructInit(&TIM_ICInitStructure);
? ? TIM_ICInitStructure.TIM_Channel = TIM_Channel_1 | TIM_Channel_2;
? ? TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
? ? TIM_ICInitStructure.TIM_ICFilter = 0; // 濾波
? ? TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
? ? TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
? ? TIM_ICInit(TIM8, &TIM_ICInitStructure);
/*
? ? TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
? ? TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
? ? TIM_ICInitStructure.TIM_ICFilter = 0; // 濾波
? ? TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
? ? TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
? ? TIM_ICInit(TIM8, &TIM_ICInitStructure);
*/
? ? TIM_ClearFlag(TIM8, TIM_FLAG_Update);? ? ? ? //清除TIM更新標(biāo)志位
? ? TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);? ?//使能開啟TIM中斷
? ? TIM_SetCounter(TIM8, 0);
? ? TIM_Cmd(TIM8, ENABLE);
使用的編碼器如下: