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.
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.
/* USER CODE BEGIN (1) */ #include "eqep.h" #include "sys_core.h" #include <stdio.h> /* USER CODE END */ // ... void main(void) { /* USER CODE BEGIN (3) */ // uint16 deltaT = 0U; // float velocity = 0U; /* EQEP initialization based on GUI Configuration. */ QEPInit(); /* Enable Position Counter */ eqepEnableCounter(eqepREG1); /* Enable Unit Timer. */ eqepEnableUnitTimer(eqepREG1); /* Enable capture timer and capture period latch. */ eqepEnableCapture(eqepREG1); while(1) { /* 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; } } /* USER CODE END */ }
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.
Top Comments