我用stm32编码器模式测角度,每次回原点读数都不为0,附上代码,请问是我程序的问题?还是增量式编码器本身就测不了角度?[mw_shl_code=applescript,true]extern u16 count1;
extern u16 count2;
//编码器1初始化,使用定时器3
void coder1_init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_Structure;
GPIO_Structure.GPIO_Mode=GPIO_Mode_AF;
GPIO_Structure.GPIO_OType=GPIO_OType_PP;
GPIO_Structure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
GPIO_Structure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Structure.GPIO_Speed=GPIO_High_Speed;
GPIO_Init(GPIOB,&GPIO_Structure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_TIM3);
NVIC_InitTypeDef NVIC_Structure;
NVIC_Structure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_Structure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Structure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_Structure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_Structure);
TIM_DeInit(TIM3);
TIM_TimeBaseInitTypeDef TIM3_Structure;
TIM3_Structure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM3_Structure.TIM_CounterMode=TIM_CounterMode_Up;
TIM3_Structure.TIM_Period=399*4;//400线编码器
TIM3_Structure.TIM_Prescaler=0;
TIM_TimeBaseInit(TIM3,&TIM3_Structure);
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_BothEdge,TIM_ICPolarity_BothEdge);
TIM_ICInitTypeDef IC_Structure;
TIM_ICStructInit(&IC_Structure);
IC_Structure.TIM_ICFilter=6;
TIM_ICInit(TIM3,&IC_Structure);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM3->CNT = 800;//设置一个基数,防止出现负值
TIM_Cmd(TIM3, ENABLE);
}
//定时器中断,读取角度
void TIM1_UP_TIM10_IRQHandler()
{
// if(TIM_GetITStatus(TIM1,TIM_IT_Update)==SET)
count1=TIM3->CNT;
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
这样就可以了,编码器是可靠的可以测量位置的
一周热门 更多>