Skip to main content
ian239955
Associate III
May 8, 2013 11:08 AM UTC
Question

STM32F107 Quadrature encoder example

Posted on May 08, 2013 at 13:08

Hi, can anyone provide an example of how to set up a counter to work with 2 quadrature inputs for up/down encoder counting?

Thanks

#stm32-pulse-counter
    This topic has been closed for replies.

    7 replies

    emalund
    Associate III
    May 8, 2013 3:08 PM UTC
    Posted on May 08, 2013 at 17:08

    Quadrature encoder example....  for up/down encoder counting?

     

    this will lead you astray, ''for up/down encoder counting'' you do not need a ''quadrature encoder'' you need a ''quadrature decoder''

    Erik
    Tesla DeLorean
    Guru
    May 8, 2013 3:57 PM UTC
    Posted on May 08, 2013 at 17:57

    This is a quick blind implementation, should suffice.

    // STM32 TIM3 Encoder Decoder (PC.06:A PC.07:B) VLDiscovery - sourcer32@gmail.com
    #include ''stm32F10x.h''
    #include ''STM32vldiscovery.h''
    /**************************************************************************************/
    void RCC_Configuration(void)
    {
    // clock for GPIO and AFIO (for ReMap)
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
    // clock for TIM3
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    }
    /**************************************************************************************/
    void GPIO_Configuration(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;
    // PC.06 TIM3_CH1, PC.07 TIM3_CH2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_PinRemapConfig( GPIO_FullRemap_TIM3, ENABLE ); // Map TIM3 to GPIOC
    }
    /**************************************************************************************/
    void TIM3_Configuration(void)
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Prescaler = 0; 
    TIM_TimeBaseStructure.TIM_Period = 65535; // Maximal
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    // TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
    TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
    TIM_Cmd(TIM3, ENABLE);
    }
    /**************************************************************************************/
    int main(void)
    {
    RCC_Configuration();
    GPIO_Configuration();
    TIM3_Configuration();
    // TIM3->CNT contains current position
    while(1); // Do not exit
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Bill Lewis
    Associate III
    Apr 14, 2014 6:04 PM UTC
    Posted on April 14, 2014 at 20:04

    Will Encoder Interface Mode always use CH1 and CH2 for the pins?

    -Bill

    Tesla DeLorean
    Guru
    Apr 14, 2014 7:11 PM UTC
    Posted on April 14, 2014 at 21:11

    Will Encoder Interface Mode always use CH1 and CH2 for the pins?

    That's how it's wired. Each TIM has only one counting element.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    tanchi6806
    Visitor II
    Sep 16, 2016 4:48 PM UTC
    Posted on September 16, 2016 at 18:48

    Hi,

    Could you advise and show how the general timer (e.g TIM4) of STM32F1xx be configured as a counter which counts pulses input and with direction input to control up or down counting?

    Thanks,

    Tanchi6806

    Tesla DeLorean
    Guru
    Sep 16, 2016 5:10 PM UTC
    Posted on September 16, 2016 at 19:10

    Wouldn't the provided code pretty do that? Change pins and configuration to use TIM4 instead of TIM3

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    tanchi6806
    Visitor II
    Sep 17, 2016 9:07 AM UTC
    Posted on September 17, 2016 at 11:07

    Hi,

    I modified the code as attached. TIM4 reads no problem with encoder A/B signals applied to pins PD12/PD But it reads only 0~65535 (or 0~1 with different setting), when pulse and direction signals are applied. What else should TIM4 should be configured in order TIM4 can count the pulse up or down controlled by a direction signal?

    tanchi6806

    ________________

    Attachments :

    puDir_Init.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0rR&d=%2Fa%2F0X0000000bee%2FvM65hZtzyZxnVLveKLsz8Aww5mE_xztRD2IrAQ2Kahw&asPdf=false