A summary of incremental encoder process detection using Stm32-band Quadrature encoder

Source: Internet
Author: User

As the operation of the frequency upgrade machine needs automatic leveling function, so start to do this work. Hardware selection is incremental encoder, 100 pulses per revolution, and later learned that each timer of the STM32 Channel 1 and Channel 2 built-in Quadrature encoder module, can be used directly. Before the company engineers are using a timer to capture the pulse, and then self-processing, I looked at the code is very troublesome, now with the stm32 of the feeling is much easier. Found the official software description, looked at the online example, an afternoon basically in my system architecture to add this device, and then initialize this device, set up the upper interface API. Finally look at some examples to extend the 16-bit counter software to 32-bit. The first step of the basic module has been successfully completed. In the future, we need to combine the collected data with the floor to make a good data structure, which is convenient to call and maintain.

My basic ideas and related software code are posted below.


Step One: Learn what things are incremental encoders. This needs Baidu, this self-study. Also very well understood.


The second step: see the online ancestors have actual combat experience, mainly in the Stm32 Forum, 21IC Forum. Baidu Related keywords can be found.


The third step: analyze the principle that the Stm32 module uses the orthogonal code detection. This part is made up of an official manual, or Baidu.

The following is the relevant initialization code, because the online example is basically used TIM3, and I use the TIM1, so I need to refer to the modified.

void Initializeencoder (void) {tim_timebaseinittypedef tim_timebasestructure;        Tim_icinittypedef tim_icinitstructure;        /* Encoder unit connected to TIM1, 4X mode */Gpio_inittypedef gpio_initstructure;        Nvic_inittypedef nvic_initstructure; /* TIM1 Clock Source Enable */Rcc_apb2periphclockcmd (RCC_APB2PERIPH_TIM1, enable),//enable TIM1 clock/* Enable GPIO A, Clock */Rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOA, enable);//enable Gpioa clock Gpio_structinit (&gpio_inits        Tructure); /* Configure pa.06,07 as Encoder input */Gpio_initstructure.gpio_pin = Gpio_pin_8 |        Gpio_pin_9;        Gpio_initstructure.gpio_mode = gpio_mode_in_floating;//pa8 PA9 floating air input gpio_init (GPIOA, &gpio_initstructure);        /* Enable the TIM1 Update Interrupt *///nvic_initstructure.nvic_irqchannel = Tim3_irqchannel;        Nvic_initstructure.nvic_irqchannel = TIM3_IRQN; Nvic_initstructure.nvic_irqchannelpreemptionpriority = timx_pre_emption_priority;        nvic_initstructure.nvic_irqchannelsubpriority = timx_sub_priority;        Nvic_initstructure.nvic_irqchannelcmd = ENABLE;        Nvic_init (&nvic_initstructure);        /* Timer configuration in Encoder mode */Tim_deinit (Encoder_timer);        Tim_timebasestructinit (&tim_timebasestructure);  Tim_timebasestructure.tim_prescaler = 0x0;  No prescaling//Set counter divider factor is 0, non-crossover//tim_timebasestructure.tim_period = (4*ENCODER_PPR)-1;                Set counter reload value tim_timebasestructure.tim_period = encoder_tim_period-1; Tim_timebasestructure.tim_clockdivision = tim_ckd_div1;//Set clock split T_dts = T_ck_int Tim_timebasestructure.tim_counter   Mode = tim_countermode_up;        TIM counts up Tim_timebaseinit (Encoder_timer, &tim_timebasestructure); Tim_encoderinterfaceconfig (Encoder_timer, Tim_encodermode_ti12, tim_icpolarity_rising, TI m_icpolarity_rising);//Use encoder mode 3 TIM_icstructinit (&tim_icinitstructure);        Tim_icinitstructure.tim_icfilter =icx_filter;//Select input comparison filter tim_icinit (Encoder_timer, &tim_icinitstructure); Clear all pending interrupts Tim_clearflag (Encoder_timer, tim_flag_update);//clear TIM1 update flag bit Tim_itconfig        (Encoder_timer, Tim_it_update, ENABLE);        Enc_clear_speed_buffer ();        Reset counter tim1->cnt = 0;        Currentcount = tim1->cnt;        Tim_cmd (Encoder_timer, ENABLE); System.Device.Encoder.Enc_GetCount = Enc_getcount;}

You can then read the value of the tim1->cnt to get the orthogonal encoded value.

This also exists the problem of insufficient number of digits, referring to the online god of the example, to understand the self-made my own changes, thanks to ancestors.

System.Device.Encoder.Enc_GetCount = Enc_getcount;
The above interface function is the data that I call to the upper application to read, probably every 1s or 10 calls once, this reference own requirements definition.

S16   enc_getcount (void) {        static  u16   lastCount = 0;        U16  Curcount = encoder_timer->cnt;//Gets the encoded value        s32 dAngle = curcount-lastcount;        if (dAngle >= max_count)        {            DAngle-= encoder_tim_period;        }        else if (DAngle <-max_count)        {            DAngle + = Encoder_tim_period;        }        LastCount = Curcount;        Return (S16) DAngle;}

The following is a definition of my own.

20141213#define encoder_timer   TIM1  //ENCODER unit connected to Tim3#define ENCODER_PPR           (U16) (+)   Number of pulses per revolution#define speed_buffer_size 8#define counter_reset   (U16) 0#define Icx_filter      (U8 ) 6//6<-> 670nsec#define timx_pre_emption_priority 1#define timx_sub_priority 0#define ENCODER_TIM_PERIOD 0xFFFF The maximum prescaler is 65536-1#define max_count 10000//10000 that is,          no more than 10,000 pulses in 1ms


The main function is the above. The call will be processed at the application level. This is built on the MSOs architecture, very practical.


Fourth step: Construct the data relationship structure of the coded counter values and floors. Wrapping them together makes the application layer invocation more convenient and will be done next.







A summary of incremental encoder process detection using Stm32-band Quadrature encoder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.