Skip to main content
JJohn.3
Associate III
November 1, 2021
Question

STM32F107 Quadrature encoder

Hello,

Regarding this post I am able to read my encoder value correctly.

 // 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)
 
{
 Print(TIM3->CNT);
}
 
 
 
 }

My problem appears when I want to know the direction and number of pulse in each direction.

For example if the counter value is zero and I change the encoder position (when the previous value is 0 )and the next value is 65532 I need to know to determine the pulse changes is 2. And if the first value is 0 and the next value is 2 how to determine the pulse and rotation direction. In addition I need to know if the previous value is 65530 and the next value is 2 how to determine the number of pulses and the direction while the value of my timer is between 0 to 65535.

This confused me. I will be grateful if you could help me in this issue.

This topic has been closed for replies.

1 reply

TDK
November 1, 2021

Save the value periodically and determine the speed by the shortest distance between readings. If the last reading was 65535 and the next is 0, rotation was 1 unit in the positive direction. Calculate both direction and use the one that is shortest.

"If you feel a post has answered your question, please click ""Accept as Solution""."