I've removed the rotary encoder from the scroll button of a defect mouse. Let's see how we can use that in a microcontroller design.

In post 1, I wired up the electronics.

In part 2, I'm capturing the encoder's info on a Cypress PSoC4.

In the third blog, I've used an Arduino.

In this one, I'm capturing the encoder's info with the eQEP module of a TI Hercules microcontroller.


texas_instruments hercules launchpad equep rotary_encoder quadrature_encoder

I'm using a Hercules LaunchPad with the RM46x microcontroller, but the approach that I'm showing works with all Hercules controllers that come with an eQEP peripheral. At the end of this post, there's a solution for processors that aren't equipped with this module.

Extract from the TI Hercules RM46 TRM


The Firmware


Just like with my previous two attempts with the Cypress PSoC 4 BLE kit and the Arduino, it is straightforward with the Hercules.

In HALCoGen, you have to configure the eQEP module.



Take care that you set the eQEP pins active in the pin multiplexing tab.



On the eQEP tab, we configure the peripheral.



That's 90% of the work done. Generate the code and switch to Code Composer Studio to write the C code.



There's really little to do there. We have to initialize the eQEP module and do regular checks to see if a new count is latched into the eQEP registers. The module does all the hard work for us.


#include "eqep.h"
#include "sys_core.h"
#include <stdio.h>

// ...
void main(void)
// uint16 deltaT = 0U;
// float velocity = 0U;
  /* EQEP initialization based on GUI Configuration. */

  /* Enable Position Counter */

  /* Enable Unit Timer. */

  /* Enable capture timer and capture period latch. */

  /* Status flag is set to indicate that a new value is latched in the QCPRD register. */
  if((eqepREG1->QEPSTS & 0x80U) !=0U)
  printf("Count %u\n", eqepREG1->QPOSLAT);

  /* Clear the Status flag. */
  eqepREG1->QEPSTS |= 0x80U;


Wiring the Hardware


Look back at my first post to check how to components are wired up.

Then connect the encoder with the LaunchPad.

Ground to Ground, 3.3V to 3.3V,

PINA to J11.39, PINB to J11.38.



Connect the LaunchPad, execute the program and Look at the CCS console.

The count is logged to the monitor on every spin.



Extract from the TI Hercules RM46 TRM



For those Hercules controllers that don't have this module, you can always use the generic I/O (GIO) module to capture the quadrature information. My friend martinvalencia has done that successfully.




Part 1: Electronics
Part 2: Capturing Input on Cypress PSoC4
Part 3: Capturing Input on an Arduino
Part 4: Capturing Input on a Texas Instruments Hercules LaunchPad with eQEP
Part 5: Capturing Input on an FPGA
Real World Application: Hercules LaunchPad and GaN FETs: Control Big Power with a Flimsy Mouse Scroll Wheel