Can Opener - ME 507
STM32Encoder Class Reference

Class which operates an STM32 timer in quadrature encoder mode. More...

#include <encoder_counter.h>

Public Member Functions

 STM32Encoder (TIM_TypeDef *timer, uint8_t pin1, uint8_t pin2)
 Set up an STM32 timer to read a quadrature encoder. More...
 
uint32_t getCount (void)
 Return the current position count from the timer. More...
 
void zero (void)
 Set the counter reading to zero.
 
void pause (void)
 Pause the counter so it won't update its count until resumed.
 
void resume (void)
 Resume the counter so it will update its count when the encoder is moved.
 

Protected Attributes

HardwareTimer * p_timer
 Pointer to the timer/counter to be used.
 

Detailed Description

Class which operates an STM32 timer in quadrature encoder mode.

This class sets up a timer/counter which is capable of reading quadrature signals from an incremental encoder and provides a method for retrieving a count which corresponds to the encoder's position. The position will overflow, and this class doesn't deal with that – the user must periodically read the position and use it to update a position count with a large enough bit width that the full position reading does not overflow.

So far this class has been tested with the following timers and pins on an STM32L476RG:

  • Timer TIM2 using pins PA0 and PA1
  • Timer TIM3 using pins PB4 and PB5
  • Timer TIM3 using pins PA6 and PA7
  • Timer TIM4 using pins PB6 and PB7
  • Timer TIM8 using pins PC6 and PC7

Constructor & Destructor Documentation

◆ STM32Encoder()

STM32Encoder::STM32Encoder ( TIM_TypeDef *  timer,
uint8_t  pin1,
uint8_t  pin2 
)

Set up an STM32 timer to read a quadrature encoder.

This class prepares an STM32 timer in quadrature encoder reading mode, counting on each transition of either channel. It only works on timers whose hardware is quadrature compatible; check the data sheet to see which are. Each compatible timer must have the encoder connected to its channel 1 and 2 inputs; these inputs can only be connected to specific pins. For most timers there are two sets of pins from which we can choose. The alternate function tables in the STM32xxx data sheet shows which pins may be used with each timer.

This class has been tested on an STM32L476RG and may or may not work on other STM32 processors; see the TODO in the constructor.

Example:

STM32Encoder timer_X (TIM3, PB4, PB5); // Set up once
...
int16_t where_am_I = timer_X.getCount (); // In a loop
Class which operates an STM32 timer in quadrature encoder mode.
Definition: encoder_counter.h:35
Parameters
timerA pointer to a TIM_TypeDef object, such as TIM3, which designates the timer to be used
pin1The pin to be configured for use with timer channel 1
pin2The pin to be configured for use with timer channel 2

Member Function Documentation

◆ getCount()

uint32_t STM32Encoder::getCount ( void  )
inline

Return the current position count from the timer.

Returns
The value in the timer's count register

The documentation for this class was generated from the following files: