In the previous guide of the timer in STM32F1(here), we took a look how to configure the timer to operate in Encoder mode, in this guide, we shall use the timer to measure the number of pulses generated by the encoder.
In this guide, we shall cover the following:
What is encoder- Required parts and connection
- Configure pins and timer to read encoder
- Download the code
- Demo
1.1 What is encoder:
A rotary encoder, also called a shaft encoder, is an electro-mechanical device that converts the angular position or motion of a shaft or axle to analog or digital output signals.
Rotary encoders are used in a wide range of applications that require monitoring or control, or both, of mechanical systems, including industrial controls, robotics, photographic lenses, computer input devices such as optomechanical mice and trackballs, controlled stress rheometers, and rotating radar platforms.
1.2 Encoder types:
- Mechanical: Also known as conductive encoders. A series of circumferential copper tracks etched onto a PCB is used to encode the information via contact brushes sensing the conductive areas. Mechanical encoders areeconomical but susceptible to mechanical wear. They are common in human interfaces such as digital multimeters
- Optical: This uses a light shining onto a photodiode through slits in a metal or glass disc. Reflective versions also exist. This is one of the most common technologies. Optical encoders are very sensitive to dust.
- On-Axis Magnetic: This technology typically uses a specially magnetized 2 pole neodymium magnet attached to the motor shaft. Because it can be fixed to the end of the shaft, it can work with motors that only have 1 shaft extending out of the motor body. The accuracy can vary from a few degrees to under 1 degree. Resolutions can be as low as 1 degree or as high as 0.09 degree (4000 CPR, Count per Revolution). Poorly designed internal interpolation can cause output jitter, but this can be overcome with internal sample averaging.
- Off-Axis Magnetic: This technology typically employs the use of rubber bonded ferrite magnets attached to a metal hub. This offers flexibility in design and low cost for custom applications. Due to the flexibility in many off axis encoder chips they can be programmed to accept any number of pole widths so the chip can be placed in any position required for the application. Magnetic encoders operate in harsh environments where optical encoders would fail to work. (from wikipedia)
1.3 Encoder Output:
In out case, the encoder shall generate pulses on both DT and CLK lines in a certain manner that can be read by the timer of stm32. In the figure below, is the output from shaft encoder when rotating clockwise. Clk is yellow and DT is Blue.
When the shaft encoder rotate clockwise, CLK like pulled high first then DT line which indicates there is rotation.
When the counter clock rotation occurs, the DT line is pulled high first then the CLK line.
2.1 Required Parts:
In this guide, we will need the following:
- STM32F103C8 (Blue pill).
- Shaft encoder (4 wire).
- TTL to USB converter.
The connection as following:
Encoder Pin | STM32F767 Nucleo-144 |
GND | GND |
+ | 3V3 |
DT | PA0 |
CLK | PA1 |
3.1 Pin configuration:
First create a source and header file with name of encoder.c and encoder.h respectively.
Within the header file, declare the following:
- void encoder_init(uint16_t max_value).
- uint16_t encoder_read(void):
#ifndef ENCODER_H_ #define ENCODER_H_ #include "stdint.h" void encoder_init(uint16_t max_value); uint16_t encoder_read(void); #endif /* ENCODER_H_ */
Within the source file and encoder_init(), we shall start the initialization sequence.
In order to read the encoder, we need to configure PA0 and PA1 as input floating:
To do this, first enable clock access to GPIOA:
RCC->APB2ENR|=RCC_APB2ENR_IOPAEN;
Then set MODE and CONF to input floating:
/*Configure PA0 as Output Alternate Push/Pull */ GPIOA->CRL&=~GPIO_CRL_MODE0; GPIOA->CRL|=(GPIO_CRL_CNF0_0); GPIOA->CRL&=~(GPIO_CRL_CNF0_1); /*Configure PA1 as Output Alternate Push/Pull*/ GPIOA->CRL&=~GPIO_CRL_MODE1; GPIOA->CRL|=(GPIO_CRL_CNF1_0); GPIOA->CRL&=~(GPIO_CRL_CNF1_1);
Thats all for GPIO.
Next, configuring timer:
3.2 Timer configuration:
In order to config the timer to read encoder, the following steps are required:
First we start by enabling clock access to TIM2 of stm32:
/*Enable clock access to timer2*/ RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;
Set the max value to be the max value passed by the user:
/*Configure timer2*/ TIM2->ARR=max_value-1;
then set bit CC1S and CC2S as following:
TIM2->CCMR1 |= (TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0 );
Then set both channel to input:
TIM2->CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
Now, we tell the TIM to operate in encoder mode as following:
TIM2->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;
Finally enable the timer:
TIM2->CR1 |= TIM_CR1_CEN;
To read the encoder, just read the count register:
uint16_t encoder_read(void){ return TIM2->CNT; }
Hence, the encoder.c file as following:
/* * encoder.c * * Created on: Jan 19, 2023 * Author: hussamaldean */ #include "encoder.h" #include "stm32f1xx.h" void encoder_init(uint16_t max_value) { RCC->APB2ENR|=RCC_APB2ENR_IOPAEN; /*Configure PA0 as Output Alternate Push/Pull */ GPIOA->CRL&=~GPIO_CRL_MODE0; GPIOA->CRL|=(GPIO_CRL_CNF0_0); GPIOA->CRL&=~(GPIO_CRL_CNF0_1); /*Configure PA1 as Output Alternate Push/Pull*/ GPIOA->CRL&=~GPIO_CRL_MODE1; GPIOA->CRL|=(GPIO_CRL_CNF1_0); GPIOA->CRL&=~(GPIO_CRL_CNF1_1); /*Enable clock access to timer2*/ RCC->APB1ENR|=RCC_APB1ENR_TIM2EN; /*Configure timer2*/ TIM2->ARR=max_value-1; TIM2->CCMR1 |= (TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0 ); TIM2->CCER &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P); TIM2->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1; TIM2->CR1 |= TIM_CR1_CEN; } uint16_t encoder_read(void){ return TIM2->CNT; }
In main.c code:
call the init function within the main function and in while loop compare the current value with the previous value as following:
#include "stm32f1xx.h" #include "encoder.h" #include "uart.h" uint16_t encoder_read_previous; int main(void) { uart2_init(); encoder_init(10); printf("hello from stm32\r\n"); while(1) { if(encoder_read()!=encoder_read_previous) { encoder_read_previous=encoder_read(); printf("Encoder counts = %d\r\n",encoder_read()); } } }
4. Code:
you may download the code from here:
Results:
Burn the code on the board and open serial terminal and set the baudrate to 115200 and rotate the encoder and you should get the following:
Happy coding 🙂
2 Comments
I cannot get timer encoder mode to work correctly on blue pill. Counts on some, but not all transitions. Does not handle jitter correctly. My code below. What am I missing?
void setup() {
RCC_BASE->APB2ENR |= RCC_APB2ENR_IOPBEN;
RCC_BASE->APB1ENR |= RCC_APB1ENR_TIM4EN;
TIMER4_BASE->ARR = 65535;
TIMER4_BASE->CCMR1 |= (TIMER_CCMR1_CC1S_INPUT_TI1 | TIMER_CCMR1_CC2S_INPUT_TI2);
TIMER4_BASE->SMCR = TIMER_SMCR_SMS_ENCODER3;
TIMER4_BASE->CR1 |= TIMER_CR1_CEN;
Serial.begin(57600);
}
void loop() {
Serial.println(TIMER4_BASE->CNT);
delay(1000);
}
Hi,
download the code from section 4 and use it as base for your code.
Add Comment