最近在调试步进电机,关于定时器读取编码器的值,有点不太明白,求指点:
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
这个函数我个人的理解是在编码器的A相和B相的上升沿,TIM3都进行计数,那么电机转一圈,TIM3的计数增量应该是编码器线数乘以2,相当于编码器分辨率变成2倍,是这样吧?但是A相和B相的相位差是90度而不是180度,所以这样的2倍分辨率其实是不准确的吧?那怎么网上例子基本都是这么用的?应该只有单相单边沿计数的1倍分辨率和双相双边沿计数的4倍分辨率才是准确的吧,还是我根本就没理解对?
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //单相单边沿计数,1倍
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge); //双相双边沿计数,4倍
另外我有个板子,TIM3读出的编码器值总是实际值的2倍,而另一个板子改用TIM2,同样的设置,读数正常,大家有遇到过吗?附上TIM3的设置
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6; //ICx_FILTER;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
// TIM_ITConfig(TIM3, TIM_IT_Trigger, ENABLE);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
|