Hi All,
I have a problem with the eqep module. The position counting misses encoder steps from time to time.
I have looked at the pulses in a oscilloscope and the signals look fine.
What can prevent the eqep module from counting position correctly? Disabling global interrupts? Wrong unit timer?
Here's my configuration the the eqep module:
/*-----------------------------------------------------------------------------
Initialization states for EQEP Decode Control Register
------------------------------------------------------------------------------*/
#define QDECCTL_INIT_STATE ( XCR_X2 + QSRC_QUAD_MODE + SWAP_ENABLE )
/*-----------------------------------------------------------------------------
Initialization states for EQEP Control Register
------------------------------------------------------------------------------*/
#define QEPCTL_INIT_STATE ( QEP_EMULATION_FREE + \
PCRM_POSMAX + \
QPEN_ENABLE + \
QCLM_POSCNT + \
UTE_ENABLE )
/*-----------------------------------------------------------------------------
Initialization states for EQEP Position-Compare Control Register
------------------------------------------------------------------------------*/
#define QPOSCTL_INIT_STATE PCE_DISABLE
/*-----------------------------------------------------------------------------
Initialization states for EQEP Capture Control Register
------------------------------------------------------------------------------*/
#define QCAPCTL_INIT_STATE ( UPPS_X8 + \
CCPS_X128 + \
CEN_ENABLE )
/*-----------------------------------------------------------------------------
Define the structure of the QEP (Quadrature Encoder) Driver Object
-----------------------------------------------------------------------------*/
typedef struct {Uint16 DirectionQep; // Output: Motor rotation direction (Q0)
Uint16 QepPeriod; // Output: Capture period of QEP signal in number of EQEP capture timer (QCTMR) period (Q0)
Uint32 PositionCount; // Output: Position counter (Q0)
Uint32 LineEncoder; // Parameter: Number of line encoder (Q0)
} QEP;
/*-----------------------------------------------------------------------------
Define a QEP_handle
-----------------------------------------------------------------------------*/
typedef QEP *QEP_handle;
/*-----------------------------------------------------------------------------
Default initializer for the QEP Object.
-----------------------------------------------------------------------------*/
#define QEP_DEFAULTS { 0x0,0x0,0x0,0x0 }
/*-----------------------------------------------------------------------------
QEP Init and QEP Update Macro Definitions
-----------------------------------------------------------------------------*/
#define QEP_INIT_MACRO(v) \
\
EQep1Regs.QDECCTL.all = QDECCTL_INIT_STATE; \
EQep1Regs.QEPCTL.all = QEPCTL_INIT_STATE; \
EQep1Regs.QPOSCTL.all = QPOSCTL_INIT_STATE; \
EQep1Regs.QUPRD = 600000; /* Unit Timer for 100Hz*/ \
EQep1Regs.QCAPCTL.all = QCAPCTL_INIT_STATE; \
EQep1Regs.QPOSMAX = 4*v.LineEncoder; \
\
EALLOW; /* Enable EALLOW*/ \
GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 1; /* GPIO20 is EQEP1A */ \
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 1; /* GPIO21 is EQEP1B */ \
EDIS; /* Disable EALLOW*/
#define QEP_MACRO(v) \
\
\
/* Check the rotational direction */ \
v.DirectionQep = EQep1Regs.QEPSTS.bit.QDF; \
\
/* Check the position counter for EQEP1 */ \
v.PositionCount = EQep1Regs.QPOSCNT; \
\
/* Check unit Time out-event for speed calculation: */ \
/* Unit Timer is configured for 100Hz in INIT function*/ \
if(EQep1Regs.QFLG.bit.UTO == 1) \
{ \
/***** Low Speed Calculation ****/ \
if((EQep1Regs.QEPSTS.bit.COEF || EQep1Regs.QEPSTS.bit.CDEF)) \
{ /* Capture Counter overflowed, hence do no compute speed*/ \
EQep1Regs.QEPSTS.all = 0x000C; \
} \
else if(EQep1Regs.QCPRDLAT!=0xffff) \
/* Compute lowspeed using capture counter value*/ \
v.QepPeriod = EQep1Regs.QCPRDLAT; \
}