在使用编码器模式的时候,参考别人的代码,上面设置CC1P和CC1NP的时候出现了问题,感觉有点矛盾,小白第一次发帖,还望大佬们能够支援一下,均配置成0的话,计数器只会向一个方向计数
下面是代码,不知道哪里出现了问题,在配置通道的时候,为什么只配置一个通道呀,如果两个都配置的话,就没有数据了
void My_TIMER_Init(void)
{
uint16_t tmpsmcr = 0;
uint16_t tmpccmr1 = 0;
uint16_t tmpccer = 0;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure2;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);//
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 2048*4; //
TIM_TimeBaseStructure.TIM_Prescaler = 0; //
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
//配置编码器接口模式
tmpsmcr = TIM4->SMCR;
tmpccmr1 = TIM4->CCMR1;
tmpccer = TIM4->CCER;
tmpsmcr &= (uint16_t)~TIM_SMCR_SMS; //
tmpsmcr |= 0x0003; // SMS = 011
tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR1_CC2S);
tmpccmr1 |= ((uint16_t)TIM_CCMR1_CC1S_0 | (uint16_t)TIM_CCMR1_CC2S_0); //设置CC1S 和 CC2S
tmpccer &= ((uint16_t)~TIM_CCER_CC1P) & ((uint16_t)~TIM_CCER_CC2P); // 设置CC1P 和 CC2P
tmpccer &= ((uint16_t)~TIM_CCER_CC1NP & (uint16_t)~TIM_CCER_CC2NP); //
tmpccmr1 &= 0x0F0F; //
TIM4->SMCR = tmpsmcr;
TIM4->CCMR1 = tmpccmr1;
TIM4->CCER = tmpccer;
TIM_EncoderInterfaceConfig(TIM5,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising); //配置了 CC1S CC2S CC1P CC2P SMS
TIM_ICStructInit(&TIM_ICInitStructure);
//* TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
//TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
// TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
// TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;*/
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(TIM5,&TIM_ICInitStructure);
//* TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
//TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
// TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
//TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
//TIM_ICInitStructure.TIM_ICFilter = 0;
// TIM_ICInit(TIM4,&TIM_ICInitStructure);*/ //这里有想配置两个通道,但试了下,不行,
/* TIM4->SMCR = tmpsmcr;
TIM4->CCMR1 = tmpccmr1;
TIM4->CCER = tmpccer;*/
NVIC_InitStructure2.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);
TIM_ClearFlag(TIM5, TIM_FLAG_Update);//
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);//
TIM5->CNT = 0;//
TIM_Cmd(TIM5, ENABLE); //
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
用编码器的话,应该可以了,我们没有用驱动器差分信号转换过来的脉冲,不好分析。
一周热门 更多>