You can interface Quadrature (Rotary) Encoders with the Hercules family via the eQEP module.
This blog explores how to do that for a Hercules TMS570LC43x LaunchPad, and the encoder from a mouse scroll wheel.
The encoder in my mouse (and probably also in yours; it's a very common part) is a EC101102X2E-VAX.
Photo taken from Soundwell.
The electronics wire up of the encoder isn't difficult. We'll just need to provide two debounce caps and two pull-up resistors:
All these pins can be connected to LaunchPad header J10:
- GND J10.2
- +3.3V J10.3
- A J10.21
- B J10.22
Activate the following two drivers:
eQEP2, the module that we connect our encoder to.
SCI1, to log the activities to a serial monitor
Activate EQEP on the PINMUX tab
The SCI1 tab doesn't need changes. We accept the default.
On EQEP2, replicate these settings:
If you want the eQEP module to start mid-range (0x00000708), also select Enable Software Initialization.
That's all in HALCoGen. Let it generate your project and switch to Code Composer Studio.
Code Composer StudioCreate your project, and take care that you do the right things for your LaunchPad.
Then add this code to your HL_sys_main.c:
// ...
/* USER CODE BEGIN (1) */
#include "HL_eqep.h"
#include "HL_sci.h"
#include "HL_sys_core.h"
#include <stdlib.h>
/* USER CODE END */
// ...
/* USER CODE BEGIN (2) */
unsigned char
/* USER CODE END */
// ...
/* USER CODE BEGIN (3) */
uint32_t NumberOfChars;
sciInit(); //Initializes the SCI (UART) module
QEPInit();
/* Enable Position Counter */
eqepEnableCounter(eqepREG2);
/* Enable Unit Timer. */
eqepEnableUnitTimer(eqepREG2);
/* Enable capture timer and capture period latch. */
eqepEnableCapture(eqepREG2);
while(1)
{
/* Status flag is set to indicate that a new value is latched in the QCPRD register. */
if((eqepREG2->QEPSTS & 0x80U) !=0U)
{
NumberOfChars = ltoa(eqepREG2->QPOSLAT,(char
sciSend(sciREG1, NumberOfChars, command); //Sends the ambient light sensor data
sciSend(sciREG1, 2, (unsigned char
/* Clear the Status flag. */
eqepREG2->QEPSTS |= 0x80U;
}
}
/* USER CODE END */
Compile and Run.
You can open a serial monitor (there is one in CCS).
Settings are:
9600 baud, 8 data bits, 2 stop bits, no parity (check on the HALCoGen SCI1 tab why these are our setting).
The CCS and HALCoGen project are attached.
Comments
Please log in or sign up to comment.