Hi all,
I purchased a EK-TM4C1294XL LaunchPad to use as the brains for a PID controlled DC motor. I've managed to get the software running which drives the motor itself, but I can't seem to get the software which interprets the encoder to work. I've hooked up the first channel of the encoder to PL1 and the second channel to PL2 and ran the code, but nothing really provides results. The direction is given to me as "1", which would suggest it is going forwards, but the value doesn't change when I change the direction, nor can I read anything regarding velocity.
My code is shown below, any help would be appreciated!
/*Global includes, definitions and functions.*/ #include <stdint.h> #include <stdbool.h> #include <math.h> #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_qei.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/debug.h" #include "driverlib/pwm.h" #include "driverlib/pin_map.h" #include "driverlib/qei.h" volatile int32_t ui32QEIDirection; volatile uint32_t ui32QEIVelocity; volatile uint32_t ui32QEIPosition; int main(void) { volatile uint32_t ui32SysClkFreq; uint32_t yolo = 0; /* Set the clock to run at 120MHz.*/ ui32SysClkFreq = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); /* Enable peripherals.*/ SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL); SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI0); SysCtlDelay(10); /* Configure GPIO pins.*/ GPIOPinConfigure(GPIO_PL1_PHA0 | GPIO_PL2_PHB0); GPIOPinTypeQEI(GPIO_PORTL_BASE, GPIO_PIN_1 | GPIO_PIN_2); /* Configure QEI.*/ /* Disable everything first.*/ QEIDisable(QEI0_BASE); QEIVelocityDisable(QEI0_BASE); QEIIntDisable(QEI0_BASE, (QEI_INTERROR | QEI_INTDIR | QEI_INTTIMER | QEI_INTINDEX)); /* Configure the QEI to capture on both A and B, not to reset when there is an index pulse, configure it as a quadrature encoder, and doesn't swap signals PHA0 and PHB0 and set the maxmimum position as 1999. (MAX_POS_CAPTURE_A_B = (ppr*4)-1.'*/ QEIConfigure(QEI0_BASE, (QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_NO_RESET | QEI_CONFIG_QUADRATURE | QEI_CONFIG_NO_SWAP), 1999); SysCtlDelay(10); QEIVelocityConfigure(QEI0_BASE, QEI_VELDIV_16, 40000); SysCtlDelay(10); QEIPositionSet(QEI0_BASE, 999); /* Enable what needs to be enabled.*/ QEIEnable(QEI0_BASE); SysCtlDelay(10); QEIVelocityEnable(QEI0_BASE); SysCtlDelay(10); /* Loop forever.*/ while(1) { /* Get direction (1 = forward, -1 = backward).*/ ui32QEIDirection = QEIDirectionGet(QEI0_BASE); /* Get velocity.*/ ui32QEIVelocity = QEIVelocityGet(QEI0_BASE); if(ui32QEIDirection == 1) { yolo++;//To see if my breakpoint is working. } /* Delay for certain amount of time.*/ //SysCtlDelay(3000); } }