cancel
Showing results for 
Search instead for 
Did you mean: 

Quadrature encoders on STM32F4

michaelmichael9137
Associate II
Posted on April 29, 2013 at 14:47

Hi everyone and thanks in advance for your advices.

I'm using a quadrature encoder from Bourns :

http://www.bourns.com/data/global/pdfs/pecpdf#search=%22PEC11%22

with an STM32F4 chip. I have two questions : - First of all, is it possible to use multiple encoders with a reasonable amount of used pins on the STM32? Maybe with shift registers or dedicated components? I have to implement 9 encoders for my project and I can't use 9 different timers to receive the datas. - My second question is about the code I already wrote. It works and ables me to read the encoder position but when I turn it too fast, it seems that I loose some datas. Any help with that? Here is the code :

#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_tim.h''
//-------------------------------------------#DEFINE------------------------------------------
// | TIM4_CH1 pin (PB.06) |
// | TIM4_CH2 pin (PB.07) |
// --------------------------
//PINS CODEUR A ET B
#define Codeur_A GPIO_Pin_6
#define Codeur_A_SOURCE GPIO_PinSource6
#define Codeur_B GPIO_Pin_7
#define Codeur_B_SOURCE GPIO_PinSource7
#define Codeur_GPIO GPIOB
#define Codeur_RCC RCC_AHB1Periph_GPIOB
#define Codeur_AF GPIO_AF_TIM4
//TIMER UTILISE
#define Codeur_TIMER TIM4
#define Codeur_TIMER_CLK RCC_APB1Periph_TIM4
#define Codeur_COUNT() Codeur_TIMER->CNT
//LEDS DEMOBOARD
#define Led_Red GPIO_Pin_12
#define Led_Green GPIO_Pin_14
#define Led_GPIO GPIOD
#define Led_RCC RCC_AHB1Periph_GPIOD
//-------------------------------------------VARIABLES------------------------------------------
//GLOBAL
volatile
int16_t Codeur_count;
volatile
int32_t Codeur_total;
//LOCAL
static
volatile
int16_t Codeur_old;
static
volatile
int16_t Codeur_actual;
//------------------------------------------PROTOTYPES-----------------------------------------
void
RCC_Configuration(
void
);
void
GPIO_Configuration(
void
);
void
TIMER_Configuration(
void
);
void
CODEUR_Reset(
void
);
void
CODEUR_Read(
void
);
//---------------------------------------------MAIN--------------------------------------------
int
main(
void
)
{
//-----VARIABLES-----//
//-----INITIALISATIONS-----//
RCC_Configuration();
GPIO_Configuration();
TIMER_Configuration();
//-----BOUCLE-----//
while
(1)
{
CODEUR_Read();
}
return
0;
}
//-------------------------------------------FONCTIONS-------------------------------------------
void
CODEUR_RESET (
void
)
{
__disable_irq();
Codeur_old = 0;
Codeur_total = 0;
TIM_SetCounter (Codeur_TIMER, 0);
CODEUR_Read();
__enable_irq();
}
void
TIMER_Configuration(
void
)
{
// set them up as encoder inputs
// set both inputs to rising polarity to let it use both edges
TIM_EncoderInterfaceConfig (Codeur_TIMER, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_SetAutoreload (Codeur_TIMER, 0xffff);
// turn on the timer/counters
TIM_Cmd (Codeur_TIMER, ENABLE);
// Reset
CODEUR_RESET ();
}
void
CODEUR_Read (
void
)
{
Codeur_old = Codeur_actual;
Codeur_actual = TIM_GetCounter (Codeur_TIMER) ;
Codeur_count = Codeur_actual - Codeur_old;
Codeur_total += Codeur_count;
}
void
RCC_Configuration(
void
)
{
// Enable RCC codeur
RCC_AHB1PeriphClockCmd(Codeur_RCC, ENABLE);
// Enable RCC led
RCC_AHB1PeriphClockCmd(Led_RCC, ENABLE);
// Enable Timer 4 clock
RCC_APB1PeriphClockCmd(Codeur_TIMER_CLK, ENABLE);
}
void
GPIO_Configuration(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 2 Inputs for A and B Encoder Channels
GPIO_InitStructure.GPIO_Pin = Codeur_A|Codeur_B;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Codeur_GPIO, &GPIO_InitStructure);
//Timer AF Pins Configuration
GPIO_PinAFConfig(Codeur_GPIO, Codeur_A_SOURCE, Codeur_AF);
GPIO_PinAFConfig(Codeur_GPIO, Codeur_B_SOURCE, Codeur_AF);
// Output Leds
GPIO_InitStructure.GPIO_Pin = Led_Red|Led_Green;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Led_GPIO, &GPIO_InitStructure);
}

Thanks again. #encoders-stm32f4-multiple #qei #qei
15 REPLIES 15
Posted on May 02, 2013 at 16:33

I don't see how you're going to significantly decimate the pins required. They'll need to be connected somewhere to external logic, or the STM32. Surely it's easier to use a higher pin count STM32 than throw a couple of logic chips, or FPGA/CPLD externally. The primary cost driver on the STM32 is the rated flash capacity. Use leaded packages to get PCB costs down.

Yes, I suppose you could use a shift register, and latch motion/direction, but that would appear to add more cost/complexity than you're willing to deal with.

Can you use analogue pots?

Examine similar products with your use case?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
emalund
Associate III
Posted on May 02, 2013 at 17:12

 I still need a way to handle 9 encoders without using 18 GPIO Pins though.

 

 

So you globally recommend to use external counters.

why not a $0.69 micro
kongisme
Associate II
Posted on June 13, 2013 at 03:39

pete and all other people.

I'm using STM32F407 and I'm using quadrature encoder which is high resolution linear scale and maximum 2Mhz pulse speed.

When the encoder speed is over 400khz, the data is missing.

I would like to know the stm32f407's encoder performance.

Is it possible to decode the high speed quadrature encoder, max 2Mhz ?

Thank you in advance.

gerhard_karl
Associate
Posted on November 24, 2014 at 11:42

Dear mr youngshin, 

can you please write your mail address below,

or contact me: gerhard_karl@gmx.at

THX.

beste regards!

smjoo
Associate
Posted on May 26, 2015 at 08:12

Hi.

I ask you somthing.

I have a problem when I count encoder pulse.

especially, there seems no problem when motor turns clockwise, but there are some trouble when  motor turns counterclockwise.

You says  ''I think my second problem is solved (data loss)''.

Can you give some information about how you solve the problem(data loss).

Posted on May 26, 2015 at 20:44

Ok, understand you're asking a question of people who started a thread TWO years ago, and has subsequent input by low participation individuals SIX months ago.

You might want to start a new thread and present your case better, with diagrams/schematics and plots of the types of signals being presented at the quadrature pins, both in the cases where it's working properly, and those where it is not, identifying the timer counters, and what you think they should have been.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..