STM32
直播中

李桂兰

7年用户 1432经验值
私信 关注
[问答]

浅析编码器接口的计数规则及其注意事项

编码器接口的计数规则有哪些?

使用编码器接口要注意哪些事项呢?

回帖(1)

周丽

2021-12-15 09:59:41
在STM32的高级定时器和一般定时器中有Encoder interface mode(编码器接口),TI1和TI2分别对应TIM_CH1 和TIM_CH2 通道。
一、计数规则如下:
表55的是编码器不同的信号下计数方向。





1.仅在TI1边沿计数:上升沿触发时,若对应TI2为低电平,则计数器向下计数;对应为高电平,则向上计数。如图2.在○1和○2处,TI1的上升沿对应TI2为低电平,所以计数器向上计数;同样的,在○3处则相反。
2.在TI1和TI2边沿计数:若TI1、TI2上升沿捕捉时,TI1上升边沿对应TI2位于低电平,则向上计数;TI2上升边沿对应TI1高电平,向上计数,所以为双倍。





二、功能配置
1.设置I/O口(启动时钟,工作在定时器模式下,无上拉)
2.配置定时器(选择编码器接口模式)

void Encoder_Config()
{
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
        TIM_ICInitTypeDef TIM_ICInitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOE,&GPIO_InitStructure);
        GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
        GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1);


        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
        TIM_DeInit(TIM1);
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;                        //单倍模式
        TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;
        TIM_TimeBaseStructure.TIM_Period = 500*4;                                          //计数周期
        TIM_TimeBaseStructure.TIM_Prescaler = 0;                                                //时钟分频
        TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
        TIM_ICStructInit(&TIM_ICInitStructure);
        TIM_ICInitStructure.TIM_ICFilter = 0;                                                        //滤波,0表示不滤波
        TIM_ICInit(TIM1,&TIM_ICInitStructure);
        TIM_EncoderInterfaceConfig(TIM1,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
        //设置定时器工作在编码器接口模式。
        TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
        TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
        TIM_ClearFlag(TIM1,TIM_FLAG_Update);
        TIM_SetCounter(TIM1,0);                                                                        //设置计数初值
        TIM_Cmd(TIM1,ENABLE);       
}


三 注意事项

配置硬件寄存器时,首先需要使能相应的时钟。顺序不能颠倒,否则寄存器设置无效。
伺服编码器接口,在单相时,每相的电平由另外一相决定,AB相同时捕捉时,电平由彼此决定。
编码器在AB相捕捉时,禁止使用双边沿触发。手册说明如下:
    Manual P358
   
    11: non-inverted/both edges
   
    The circuit is sensitive to both TIxFP1 rising and falling edges (capture or trigger operations
     in reset, external clock or trigger mode), TIxFP1 is not inverted (trigger operation in gated mode).        
     This configuration must not be used in encoder mode.
4 使用TI1和TI2时,需要将TI1CN和TI2CN(通道1和2的互补通道)下拉保持为低电平。详见手册。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分